I have three folders under the Test folder.

Test ----> dir1, dir2, dir3

Under dir1,dir2 and dir3, I have lot of files. I want to tar only the files not the dir1, dir2 and dir3.


In Test:

pax -w's|.*/||' dir[1-3]/* | xz > file.tar.xz

would archive the files without their path components.

Note that the dir[1-3]/* would omit hidden files. For symlinks, the -s would also rewrite the symlink targets.

If you only want the regular files in there, and assuming none of your file names contain newline characters, you could do:

find dir[1-3] -type f | pax -ws'|.*/||' | xz > file.tar.xz

If the file names may contain newline characters, on some systems, you could write:

find dir[1-3] -type f -print0 | pax -w0s'|.*/||' | xz > file.tar.xz

While find and pax are standard commands, -print0 and -0 are not so YMMV.

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.