'Tar' the result of a 'find', preserving the directory structure
9,718
You can use xargs
to feed the output of a command as arguments to another:
find . -iname '*.txt' -print0 | xargs -0 tar zcvf the_tarball.tar.gz
Note here the -print0
from find and -0
from xargs work in conjunction to delimit file names correctly (so that names with spaces and such aren't a problem).
Related videos on Youtube
Author by
Kamafeather
Updated on September 18, 2022Comments
-
Kamafeather over 1 year
I want to tar all the *.txt files that I get as the result of a find command, that exist in a directory having a tree structure like this:
- Directory_name
- dir1
- file1.pdf
- file1.txt
- dir2
- file2.pdf
- file2.txt
- dir3
- file3.pdf
- file3.txt
(the filenames are just examples).
But I want to preserve the directory structure.
What command can give me a tar.gz file with this content?
- dir1
- file1.txt
- dir2
- file2.txt
- dir3
- file3.txt
-
doktor5000 over 9 yearsxargs is not even necessary, you can pipe the output of find directly to tar via stdin. For your example:
find . -iname '*.txt' | tar zcvf the_tarball.tar.gz --files-from -
Explained here: stackoverflow.com/a/9534272/4017010 and the general topic was already discussed here: unix.stackexchange.com/questions/5641/… -
Kamafeather over 9 yearsHi, this worked perfectly. And thanks doktor5000 for the precision :)