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.
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.
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.
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.
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
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
, andtar
is writing to the file namedp
instead offile.tar
. – KT. – 2016-04-29T10:57:07.6834Also, 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
ortar -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.32714This 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 asroot
. – Vladimir Panteleev – 2016-06-01T11:33:48.4031@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