154

Any idea how to exclude a wild-carded path(s) from a command-line 7zip command?

I'm doing something like this:

7z.exe a -t7z archive.7z FolderToArchive\ -mx0

and would like to exclude any \bin\*.* or \obj\*.* folders found underneath "FolderToArchive".

To exclude files, you can use the -x parameter. The help file gives this example for using -x:

7z a -tzip archive.zip *.txt -x!temp.*

That's great for excluding a file. But, again, I would like to exclude a wildcard-specified folder. Under my "FolderToArchive" there are multiple folders, under those folders there may or may not be bin\ and obj\ folders. I would like to not include these in the archive.

I've tried patterns like

-x!bin\*
-x!bin\*.*
-x!\bin\*
-x!\bin\*.*
-x!\\bin\\*
-x!\\bin\\*.*

None seem to exclude the bin\ folder. Is this simply a limitation of 7zip?

Matthias Braun
  • 1,162
  • 1
  • 17
  • 29
Yoopergeek
  • 1,692
  • 2
  • 11
  • 10

3 Answers3

199

To exclude the bin and obj folders recursively you can use the command:

7z.exe a -t7z archive.7z FolderToArchive\ -mx0 -xr!bin -xr!obj
heavyd
  • 62,847
  • 18
  • 155
  • 177
  • 10
    For everyone else: if you want to ignore only the files in the root directory, use `-xr0!*.zip` - at least I couldn't get it to work without the `r0`. – Oliver Feb 23 '13 at 09:46
  • 9
    Also, if this is in a Linux/Unix command line, you may need to enclose in single quotes, to not add pre-processing by the shell, e.g. `'-x!$RECYCLE.BIN'` to exclude the Recycle bin of an external drive. – thanosa75 Apr 03 '17 at 11:29
  • 4
    what is -mx0 good for? – ESP32 Oct 30 '19 at 17:12
  • 3
    What does -mx0 mean, and what is the ! after -x mean? – user324747 Mar 23 '20 at 17:42
  • 2
    Windows batch files with `SETLOCAL ENABLEDELAYEDEXPANSION` require the `!` to be escaped with `^` like `-xr^^!skip_this_dir` – JimB Jan 13 '21 at 10:33
  • On Linux you need to quote the `!`: `-xr'!bin'` (NOT the `-xr` bit) – Ken Sharp Aug 05 '23 at 02:28
25

To avoid bug, use -r or -xr carefully.

suppose you have directories like:

.\path1\path2\bin
.\path1\path2\src
.\path3\path4\path5\bin
.\path3\path4\path5\src

and run the command:

7z a -t7z archive.7z .\path1\path2 .\path3\path4\path5 -xr!bin

what you got in archive.7z:

.\path2\src
.\path5\src

That is, the .\path2\ and .\path5\ became the top folder in archive.7z, and both bin directories were excluded.

-x only support path/filename relative to the top folder in archive.

So, if you only want to exclude .\path1\path2\bin, but to include all the other 'bin' directories, the command should be like this:

7z a -t7z archive.7z .\path1\path2 .\path3\path4\path5 -x!path2\bin

I tried to use absolute path in -x, but never succeed.


Update:

There is an option -spf in 7z for Linux which works with absolute path.

According to Marco, -spf is also available on Windows from 7-zip 15.14

zhazha
  • 361
  • 3
  • 5
  • 3
    Note: At least on Ubuntu you are best advised to put all those -x terms in single quotes `'-x!path2\bin'` to not run into shell substitution... (getting you a much longer, syntactically incorrect “command”...) – Frank N Sep 18 '17 at 08:45
  • To refer to @FrankNocke 's comment, you can also do this on windows to allow dots in folder names: `'-xr!\.foo'` – DonBecker May 11 '18 at 18:06
2

Based on @zhazha's answer and make it more clear about how to exclude sub folders exactly. On Windows 10, to backup a Visual Studio solution root folder:

D:\VS2019\Sln1

then:

//goto the parent of the root folder first which make thing clearer
//not sure what will happen if you go into the root folder
cd D:\VS2019
"<path-of-7z>\7z.exe" a -tzip -mx0 Sln1_backup.zip Sln1 -x!Sln1\.vs -x!Sln1\Debug -x!Sln1\Release -x!Sln1\lib -x!Sln1\Project1\x64 -x!Sln1\Project2\obj

Then you can be sure only the specified sub folders are excluded, for example Sln1\Project3\obj or Sln1\Project4\lib will not be excluded unintentionally. Works for the hidden huge .vs folder.

-mx0 means archive/no compression.

jw_
  • 762
  • 1
  • 10
  • 21