How to tar a directory preserving not only permissions, but ownership too

41

12

I have to compress a directory using tar.gz preserving not only permissions, but ownership/groups too.

And, in this directory there are many files that belong to many users.

Paulo Coghi - Reinstate Monica

Posted 2014-11-10T23:40:56.247

Reputation: 664

Answers

43

You're looking for the -p flag so an example would be tar -cvpf file.tar folderToCompress, be careful using the tar command as it is easy to overwrite files if your syntax for the command is incorrect.

The owners of the file is preserved normally, when extracting you need to use --same-owner flag. Such as tar --same-owner -xvf file.tar although the flag is only recommended for super users.

Check the tar man page.

SupaJord

Posted 2014-11-10T23:40:56.247

Reputation: 741

9It has to be tar -cvpf file.tar (or perhaps better yet in terms of clarity, -cvp -f file.tar). Otherwise the -fp part is interpreted as --file p, and tar is writing to the file named p instead of file.tar. – KT. – 2016-04-29T10:57:07.683

4Also, given that the answer mentioned compression and people tend to copy-paste answers from posts without thinking anyway, let me note that the popular archiving idiom with compression would be: tar -czvpf file.tar.gz folderToCompress or tar -cjvpf file.tar.bz2 folderToCompress. – KT. – 2016-04-29T11:00:30.063

@KT - you are correct, so I fix the -f flag -- although I didn't fix the compression -z flag. For compression, I'd recommend -Ipigz (that's a capital i) in lieu of -z; on multi-core systems, pigz can be considerably faster. – NVRAM – 2016-05-09T17:38:42.327

14This answer is wrong. p is an extraction flag, it will have no effect at archive creation. It also affects file permissions, not ownership. The respective flag for ownership is --same-owner, which is enabled by default when extracting as root. – Vladimir Panteleev – 2016-06-01T11:33:48.403

1@NVRAM SnowRep undid your changes (!!!). SnowRep, I'm downvoting you and restoring NVRAM's edit, which is correct. Please do not intentionally put incorrect info on the site! – Kyle Strand – 2016-09-27T23:24:31.493

@VladimirPanteleev , you are right. If you ant, you can post a new answer and I'll set it as accepted. – Paulo Coghi - Reinstate Monica – 2017-01-13T12:54:10.867

25

I have to compress a directory using tar.gz preserving not only permissions, but ownership/groups too.

By default, tar will preserve file permissions and ownership when creating the archive.

To extract file permissions and ownership, you will need to run tar as root when extracting, since changing file ownership usually requires superuser privileges. See this question for more information.

Vladimir Panteleev

Posted 2014-11-10T23:40:56.247

Reputation: 775

1When you say it preserves ownership, does that mean both the user and the group? – CMCDragonkai – 2016-08-01T10:56:20.253

1That's correct. Both the owner and group are recorded by default (at least in GNU tar). – Vladimir Panteleev – 2016-08-01T13:26:21.527