Create a single tar file for multiple directories by excluding its parent folders Create a single tar file for multiple directories by excluding its parent folders shell shell

Create a single tar file for multiple directories by excluding its parent folders


If I understand right the question, you can use the -C (capital C = change directory) option, e.g:

tar cvf /tmp/some.tar -C /path/to/dir1 . -C /path/to/dir2 .    #multiple -C allowed

check it with

 tar cf - -C /path/to/dir1 . -C /path/to/dir2 .  | tar tvf -

Example:

createing a testcase

cd /tmpmkdir testcd testmkdir -p {dir{1..3},testdir}touch dir1/file{1..3} dir2/file{4..6} dir3/file{7..9}

the tree is now:

$ find . -print../dir1./dir1/file1./dir1/file2./dir1/file3./dir2./dir2/file4./dir2/file5./dir2/file6./dir3./dir3/file7./dir3/file8./dir3/file9./testdir

The tar:

tar cf - -C dir1 . -C ../dir2 . -C ../dir3 . | tar tvf -

the tar content is:

drwxr-xr-x  0 jm    staff       0 14 aug 13:07 ./-rw-r--r--  0 jm    staff       0 14 aug 13:07 ./file1-rw-r--r--  0 jm    staff       0 14 aug 13:07 ./file2-rw-r--r--  0 jm    staff       0 14 aug 13:07 ./file3drwxr-xr-x  0 jm    staff       0 14 aug 13:08 ./-rw-r--r--  0 jm    staff       0 14 aug 13:10 ./file4-rw-r--r--  0 jm    staff       0 14 aug 13:10 ./file5-rw-r--r--  0 jm    staff       0 14 aug 13:10 ./file6drwxr-xr-x  0 jm    staff       0 14 aug 13:08 ./-rw-r--r--  0 jm    staff       0 14 aug 13:10 ./file7-rw-r--r--  0 jm    staff       0 14 aug 13:10 ./file8-rw-r--r--  0 jm    staff       0 14 aug 13:10 ./file9

If you want something other, please edit your question.