How to Pass Wildcards with Spaces to unrar

This article explains how to correctly pass wildcards containing spaces to the unrar command-line utility. When dealing with archives or internal files whose names contain both spaces and wildcard characters, command-line shells often split arguments incorrectly or expand wildcards prematurely. By using proper quoting and escaping techniques, you can ensure unrar processes the pattern as intended across Linux, macOS, and Windows environments.

The Problem: Shell Expansion vs. Internal Matching

When you type a command in a terminal, the shell (such as Bash, Zsh, or PowerShell) attempts to process spaces as argument delimiters and asterisks (*) as file expansion patterns before passing the arguments to the unrar executable. If the shell expands the pattern or splits it at the space, unrar receives broken arguments and returns errors such as "No files to extract" or "Cannot open file."

Handling Wildcards in Archive File Names

If you want to extract multiple RAR archives that contain spaces in their file names (for example, My Backup 2023.rar, My Backup 2024.rar), you need to prevent the shell from breaking the filename at the space while allowing either the shell or unrar to evaluate the wildcard.

On Linux and macOS, enclose the entire pattern in double quotes:

unrar x "My Backup *.rar"

Alternatively, escape the spaces with backslashes while leaving the wildcard unquoted if you want the shell to handle the expansion:

unrar x My\ Backup\ *.rar

On Windows (Command Prompt), enclose the argument in double quotes:

unrar x "My Backup *.rar"

Handling Wildcards for Files Inside the Archive

If you want to extract specific files that contain spaces from inside an archive, you must prevent the shell from expanding the wildcard locally. Instead, the literal pattern must be passed directly to unrar.

To extract files matching a pattern like Report Q1 *.pdf from data.rar, use single quotes on Linux and macOS to disable shell expansion completely:

unrar x data.rar 'Report Q1 *.pdf'

If you use double quotes in Bash, escape the wildcard with a backslash to prevent premature expansion:

unrar x data.rar "Report Q1 \*.pdf"

On Windows Command Prompt, single quotes are not recognized as string delimiters, so use double quotes:

unrar x data.rar "Report Q1 *.pdf"

In Windows PowerShell, wrap the argument in single quotes to pass the string literally to the executable:

unrar x data.rar 'Report Q1 *.pdf'