Bash Shell One Liner: Find and compress files

Mostly wanted to write this down so I can find it later…

This bash shell snippet will search a directory tree for files matching a number of regular expression criteria, and then add them all to tar/gzipped file.

find -X . -name "*201009*" -o -name "*201010*" -o -name "*201011*" | xargs tar czvf ~/Desktop/corplogs.tgz

I was using the Mac OS X terminal…this took me a long time to figure out because all the find tutorials on the web have the find criteria inside parentheses.  Apparently the parentheses aren’t necessary in the Mac terminal.

In this case, I was looking for log files between September 2010 and November 2010.  The “-o” argument is a logical OR.  The find command spits out a list of file names, and xargs bundles them up and passes them to the tar command.

About these ads