If you want to produce a zipped tar file (.tgz
) and want to avoid problems with spaces in filenames:
find . \( -name \*.php -o -name \*.html \) -print0 | xargs -0 tar -cvzf my_archive.tgz
The -print0
“primary” of find
separates output filenames using the NULL (\0
) byte, thus playing well with the -0
option of xargs
, which appends its (NULL-separated, in this case) input as arguments to the command it precedes.
The parentheses around the two -name
primaries are needed, because otherwise the -print0
would only output the filenames of the second -name
(there is no implied printing if -print
or -print0
is present, and these only have an effect if they are evaluated).
If you need to skip some filenames or directories (e.g., the node_modules
directory if you work with Node.js), prepend one or more -prune
primaries like this:
find . -name skipThisName -prune -o \
-name skipThisOtherName -prune -o \
\( -name \*.php -o -name \*.html \) -print0 | xargs -0 tar -cvzf my_archive.tgz