I have below paths in my system


In "z" directory there are some files i just want to make tar of that files without going into directory .

  • 1
    Do you want to preserve the rest of the directory structures? Like should the tar contain a/b/z/file1 and e/f/z/file2 or just file1 and file2? Nov 23, 2015 at 15:26

2 Answers 2


Let's assume your tarfile is $TARBALL with an absolute path.

tar cfC $TARBALL /a/b/z .
tar AfC $TARBALL /e/f/z . 

The -C option changes directory to the next argument. What's not clear is what will happen when files of the same name are in both directories.

The alternative is to keep the path names:

tar cf $TARBALL /a/b/z /e/f/z

The pathnames will be retained and kept in the tarball and those directories will be created during extraction.


Suppose your files are like


And you want to compress dir2 and a.txt from dir3, then you can use

tar -cvf mytarfile.tar /home/usr/dir1/dir2/ /home/usr/dir1/dir3/a.txt

Or just do

tar -cvf mytarfile.tar dir2 dir3/a.txt

staying in directory dir1 where dir2 and dir3 reside.

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.