Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tar with --include pattern

Tags:

tar

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?

like image 493
nhed Avatar asked Apr 21 '11 17:04

nhed


People also ask

Does tar include symbolic links?

The archive command tarBy default, the tar utility archives the symbolic links such that they can be restored when the archive is unpacked and saves hard-linked files only once within the archive. From the size of the file archive.

How will you convert a directory in to one single archive file?

In Unix and Unix-like operating systems (such as Linux), you can use the tar command (short for "tape archiving") to combine multiple files into a single archive file for easy storage and/or distribution.


2 Answers

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 - 
like image 198
Nicholas Sushkin Avatar answered Sep 29 '22 06:09

Nicholas Sushkin


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.

like image 29
cnicutar Avatar answered Sep 29 '22 05:09

cnicutar