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.
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this communityThe owners of the file is preserved by default.
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.
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
.
tar -czvpf file.tar.gz folderToCompress
or tar -cjvpf file.tar.bz2 folderToCompress
.
-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.
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
.
Jun 1, 2016 at 11:33
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.