Wednesday, August 22, 2007

Recursively Excluding Directories with Info-ZIP

You want to compress the content of a directory, ignoring all the .svn files created by Subversion (or the .cvs files from CVS). Info-ZIP unfortunately doesn't have a simple options to do that. So instead, here is what you can do:

find . -name .svn -exec echo {}/* \; > /tmp/exclude.txt; zip -q9r archive.zip * -x@/tmp/exclude.txt

With find, you first output the path to all the .svn directories into a temporary file, adding "/*" at the end of the path. Then you pass that file to zip with the -x@ option.

This works just fine, but is there a better way, maybe without using a temporary file, with everything in one line?

Update November 1, 2007 - The following, suggested by Gavin in the comments of this post, works like a charm and fits on one line. Here we are compressing the content of a WEB-INF directory to create a WAR file:

find WEB-INF -path "*/.svn" -prune -o -print | xargs zip myapp.war

Update February 16, 2009 - Tastenmetzger suggested in the comment an even simpler way of achieving the same result, using just the zip command:

zip -r myzip.zip * -x *.DS_Store *.svn*

Isn't that beautiful?