How to Extract Files Matching Wildcard Pattern from Zip Ubuntu
This guide explains how to extract specific files from a zip archive using wildcard patterns on Ubuntu. You will learn the correct syntax for the unzip command, how to use asterisks and question marks for matching, and why quoting your patterns is essential to prevent shell expansion errors.
Using the Unzip Command with Wildcards
The standard tool for handling zip files in Ubuntu is
unzip. To extract only files that match a specific pattern,
you append the pattern to the command. Wildcards allow you to select
multiple files without typing every name individually.
The asterisk (*) represents any number of characters,
and the question mark (?) represents a single
character.
Basic Syntax
unzip archive.zip 'pattern'Extracting Files by Extension
To extract all text files from an archive, use the asterisk wildcard before the extension. You must enclose the pattern in single quotes to prevent the Ubuntu shell from expanding the wildcard before passing it to the unzip command.
unzip archive.zip '*.txt'Extracting Files by Name Prefix
If you need to extract files that start with a specific name, place the asterisk at the end of the pattern.
unzip archive.zip 'report*'This command extracts report.pdf,
report_final.docx, and any other file beginning with
“report”.
Extracting Files from Specific Directories
You can also match paths within the zip archive. To extract all
images from a folder named images, use the following:
unzip archive.zip 'images/*.jpg'Important Note on Quoting
Always wrap your wildcard pattern in quotes (either single
' or double "). If you omit the quotes, the
Ubuntu shell tries to match files in your current working directory
instead of inside the zip archive. This often results in an error
stating that the file does not exist.
# Incorrect: Shell expands * before unzip sees it
unzip archive.zip *.txt
# Correct: Unzip handles the expansion
unzip archive.zip '*.txt'Verifying Contents Before Extraction
If you are unsure which files match your pattern, list the contents
of the zip archive first using the -l flag. This allows you
to test your wildcard pattern without extracting any data.
unzip -l archive.zip '*.txt'Once you confirm the list matches your expectations, run the
extraction command without the -l flag.