Exclude File Extensions Using 7-Zip Command Line

When creating archives with the 7-Zip command-line tool, you can filter out unwanted files by using the exclusion switch. This guide explains the -x switch syntax, demonstrates how to exclude single or multiple file extensions, and covers recursive exclusions across subdirectories.

The Exclusion Switch: -x

The primary switch used to exclude files in 7-Zip is -x. To target file extensions specifically, pair the switch with an exclamation point (!) followed by a wildcard pattern representing the extension.

Basic Syntax:

7z a <archive_name> <source_path> -x!*.<extension>

Excluding Extensions Recursively: -xr

By default, the -x switch only applies to the root directory specified in your command. To exclude file extensions across all subdirectories, use the recursive variant -xr.

Example: Exclude .log files across all folders

7z a backup.7z "C:\MyProject\*" -xr!*.log

Excluding Multiple File Extensions

To exclude multiple file extensions simultaneously, repeat the switch for each extension you want to omit.

Example: Exclude .log, .tmp, and .bak files

7z a backup.7z "C:\MyProject\*" -xr!*.log -xr!*.tmp -xr!*.bak

Excluding Extensions Using an Exclusion List

If you have a long list of file extensions to exclude, you can save them in a plain text file (one pattern per line) and reference it using the @ symbol.

Contents of exclude.txt:

*.log
*.tmp
*.bak
*.exe

Command:

7z a backup.7z "C:\MyProject\*" -xr@exclude.txt