tar with --include pattern

tar
nhed picture nhed · Apr 21, 2011 · Viewed 55.4k times · Source

The following is a snippet from a script I have that tries to tar up all php files in a subdir. It tries to use the '--include' parameter but does not seem to work (output is from 'set -x' in bash)

+ find . -name '*.php'
./autoload.php
./ext/thrift_protocol/run-tests.php
./protocol/TBinaryProtocol.php
...
./transport/TTransportFactory.php
+ tar -cvjf my.tar.bz2 '--include=*.php' .
+ set +x

The find found several php files but tar does not seem to see them. If I take out the --include all files are tarred.

I know I can use find to feed a list (find . -name '*.php' -print0 | tar -cvjf "my.tar.bz2" --null -T -), but whats wrong with the --include param?

Answer

Nicholas Sushkin picture Nicholas Sushkin · Oct 1, 2014

GNU tar has a -T or --files-from option that can take a file containing a list of patterns to include. The file specified with this option can be "-" for stdin. So, you can pass an arbitrary list of files for tar to archive from stdin using -files-from -. Your example becomes:

find . -name '*.php' | tar -cvjf my.tar.bz2 --files-from -