# How do I use 7-zip to backup files, but exclude some directories

I would like to use 7-zip to backup a directory, but I would like it to exclude all directories named ".svn" (anywhere in the source tree). Does anybody know if this is possible and in that case how?

-

To exclude all .svn directories you need to add the -xr!?svn\* switch

For example the following will create a backup of the C:\Project\To\Backup directory excluding any folders that satisfy ?svn:

"C:\Program Files\7-Zip\7z.exe" a -r -tzip -y -xr!?svn\* Project.zip C:\Project\To\Backup\*

-
Thanks ManiacD, but I can't get this to work -xr!?svn* gives me an error and if I try -xr!.svn* it still won't exclude .svn –  Mikael Sundberg Aug 23 '09 at 12:03
you need a backslash after ?svn for it to work that signifies to exclude everything underneath the .svn directory. -xr!?svn\* –  ManiacD Aug 23 '09 at 14:04
Don't worry I had heaps of problems initially getting it work as well. The 7-zip exclude switch -x (-xr recurse directory) with ! excludes filenames based on a wildcard search. Without the backslash it is trying to exclude filenames that match ?svn* ie. asvn.log with the \* at the end mean don't include anything underneath a directory matching ?svn –  ManiacD Aug 23 '09 at 14:18
For details on how to use wildcards have a look at the help file shipped with 7-Zip. Helped me to fix a similar task a few days ago. 7-Zip uses wildcards a bit ... different ;) –  släcker Aug 23 '09 at 15:22
And if you stumbled here looking for general folder exclusion, note that the ? is to match "period-svn" (.svn), so to to skip everything in folder SkipMe you would use -xr!SkipMe\* –  drzaus Oct 16 '13 at 21:15

Instead of using 7-Zip to exclude the .svn (or potentially _svn) folders, I would recommend using the svn export command (use svn.exe from SlikSVN) to export the working copy to a temporary folder:

svn export C:\Path\To\WC C:\Backup\Staging


Then use 7-Zip as follows:

7z.exe a "C:\Parth\To\Archive" "C:\Backup\Staging\*" -bd t7z -v2g -r


Then delete the staging folder.

This is what I do to backup my local working copies.

-
Thanks for the export tip. Available on TortoiseSVN menu too. –  Arnold Spence Jul 28 '10 at 23:00

You can exclude files with 7zip using a list of files or directories:

/path/to7Zip/7z a -bd f:/backup/backup_2009-08-23_daily.zip home \
'-xr@\path\to\backup_daily_exclude.lst'


The exclude file looks like:

home\Photos\iPod*
home\dhltd\*
BlogMatrix\Sparks\db\*.archive
home\eclipse\*
.svn


The key is the -xr and in particular the "r" which indicates apply the exclude list recursively, to each level of the directory. You may want to use 2 exclude file lists one for absolute and one for recursive exclusions. The above is from a bash script that runs in cygwin.

-

When I used

"C:\Program Files\7-Zip\7z.exe" a -r -ttar -xr!?git\* aufs2-util.tar aufs2-util\*


it ended up adding the .git directory which I didn't want, changing it to

"C:\Program Files\7-Zip\7z.exe" a -r -ttar -xr!?git\ aufs2-util.tar aufs2-util\*


got the desired result.

-

When I used

7z a "D:\codebase\w.7z" "D:\codebase\Edison\otm\Webapp" -t7z -mx0 -xr!WEB-INF\*


the WEB-INF directory was not excluded. Adding an asterisk before the dir name

7z a "D:\codebase\w.7z" "D:\codebase\Edison\otm\Webapp" -t7z -mx0 -xr!*WEB-INF\*


Got the desired result.

-

For me, I was trying to backup a bunch of maven code directories and wanted to exclude any directory named "target":

"C:\Program Files\7-Zip\7z.exe" a -r -tzip -y -xr!*\target\* site.zip C:\code\site\


Relevant exclude parameter was -xr!*\target\*.

-

## protected by studiohack♦Aug 31 '11 at 6:02

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.