How to Use Wildcards in 7-Zip File Inclusion Lists

This article explains how to properly format and use wildcards within 7-Zip file inclusion list files when working with the command-line interface. You will learn the syntax for list files, how 7-Zip processes the asterisk (*) and question mark (?) patterns, and how directory recursion switches alter how these wildcards match files across directories.

Creating an Inclusion List File

A list file is a standard plain text document (saved with UTF-8 or ANSI encoding) containing file paths and patterns to be processed, with each entry placed on a new line. When running 7-Zip from the command line, you reference this list using the @ prefix or the -i (include) switch:

7z a archive.7z @include.txt

Alternatively, to explicitly state an include list:

7z a archive.7z -i@include.txt

Supported Wildcard Characters

7-Zip supports two primary wildcard characters inside list files:

  • Asterisk (*): Represents zero or more arbitrary characters.
  • Question Mark (?): Represents exactly one arbitrary character.

Within the list file (include.txt), you can specify patterns directly:

*.docx
project_??.xlsx
src\*.cpp
images\2024_*.png

Controlling Directory Recursion with -r

By default, 7-Zip does not search subdirectories recursively for wildcard entries in a list file unless instructed or unless the path explicitly defines directory levels. The -r switch controls this behavior:

  • Without -r (Default): A pattern like *.log matches files only in the current working directory.
  • -r (Enable Recursion): Recursively searches all subdirectories for matching patterns.
    7z a archive.7z -i@include.txt -r
    If include.txt contains *.log, 7-Zip will archive every .log file in the current folder and all of its subfolders.
  • -r- (Disable Recursion): Explicitly turns off recursion, even if subfolder wildcards are used.
  • -r0 (Scoped Recursion): Recursion applies only to wildcards that include directory delimiters (e.g., logs\*.txt).

Absolute vs. Relative Paths with Wildcards

Wildcards behave differently depending on whether paths are relative or absolute:

  • Relative Paths: docs\*.pdf will search the docs folder relative to the directory where the command is executed.
  • Absolute Paths: C:\Backups\*.bak targets the exact absolute path. When using absolute paths with recursion (-r), 7-Zip traverses from the specified root downward.

Handling Spaces and Special Characters

  • Spaces: Place each path or pattern on its own line. While quotation marks are generally required in standard shell commands, in 7-Zip list files, wrapping paths containing spaces in double quotes (e.g., "My Documents\*.pdf") ensures reliable parsing.
  • Blank Lines: Empty lines inside the list file are automatically ignored by 7-Zip.
  • Comments: 7-Zip does not support comment syntax within list files; every line is interpreted as a file pattern.

Complete Working Example

To archive multiple file types across subdirectories using a list file:

  1. Create a file named patterns.txt with the following content:

    *.config
    bin\*.dll
    "Assets\Images\*.png"
  2. Run the archive command with recursive processing:

    7z a backup.7z -i@patterns.txt -r