How do I create a tar.gz from

  1. A list of FULL PATH files (such as /root/test/abcd/123.txt )
  2. that includes both directories AND files
  3. that preserves the ownership AND permissions of both files and directories when extracted?

This is how my list of files (dbfiles.log) looks like:

[oracle@test ~]$ cat dbfiles.log
[oracle@test ~]$

This is how I am doing it today:

tar -czvf TARFILENAME.tar.gz -T dbfiles

However it requires manual permission configuration after extracting. By the way, I have to use sudo to extract the file, so directories get root:root ownership and not the original oracle:oinstall as needed.

I found some references suggesting that tar's flag --no-recursion could solve the problem:

tar -zcf file.tgz --no-recursion abc --recursion abc/def

I believe this is the right approach, but I'm not sure how can I make this work with my file list. Maybe using find to go through the list and send output to the tarball? I'm honestly not sure.

  • tar -p preserves permissions
    – Panki
    Oct 15, 2020 at 11:42
  • 1
    Read man tar or info tar.
    – waltinator
    Oct 15, 2020 at 12:13

2 Answers 2


for compressing you can use tar -cvpf filename.tar.gz directory.

while extracting, use --same-owner parameter. for e.g. tar -xvzf --same-owner filename.tar.gz

  • A hostile person again removed a usefull comment. So let me add it again: The tar command does not implement an option called --same-owner, so you seem to use a tar clone. But in your example command line, any tar compatible command needs to use the argument --same-owner as argument to the f keyletter. If the tar clone you are using behaves otherwise, it is broken.
    – schily
    Oct 16, 2020 at 14:18

I end up finding the solution. Here it is in case someone else needs:

awk -F/ '{ d=$1; for (i=2; i <= NF; i++) { print d; d=d "/" $i }; print d }' dbfiles | awk '{ if (index(old,$0 "/") != 1) { old=$0; print } }' | tar czvf $LOCALFS/$TARFILENAME.tar.gz --no-recursion -T -

Thanks to who have answered my initial question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.