'tar with --include pattern
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?
Solution 1:[1]
Actually I just looked into tar(1)
on my freebsd system and I found an --include option (earlier I had looked on some old man page online). The --include
options is quite powerful. Here are some examples
These are the files
cnicutar@uranus ~/tar_test $ ls -1
a.c
b.c
x
Simple tar, archive everything
cnicutar@uranus ~/tar_test $ tar -cvf archive1.tar *
a a.c
a b.c
a x
Archive only C files
cnicutar@uranus ~/tar_test $ tar -cvf archive2.tar --include='*.c' *
a a.c
a b.c
So what is probably wrong in your script is that you give tar .
instead of .*
as the last argument.
EDIT
I have tried it and was surprised. The behavior of tar(1)
is unexpected but (I believe) intended. The man page says:
Process only files or directories that match the specified pattern.
So when you specify the pattern it filters out any directories that don't match it. So if your directories don't happen to have that extension (it's valid but uncommon) it won't descend into them (even if deep inside there might be "interesting" files).
So in conclusion I believe it would be best to use another way to recursively enumerate + filter files.
Solution 2:[2]
GNU tar has a -T or --files-from option that can take a file containing a list of files 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 -. Using find patterns to generate a list of files, your example becomes:
find . -name '*.php' -print0 | tar -cvjf my.tar.bz2 --null --files-from -
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 |