Menu
Coddy logo textTech

Combining Wildcards

Part of the Fundamentals section of Coddy's Terminal journey — lesson 28 of 82.

The real power of wildcards comes when you combine them together. You can mix *, ? and [] in a single pattern to create very precise file matching.

Given this filesystem:

photo_1.jpg
photo_2.jpg
photo_10.jpg
photo_1.png
photo_2.png
report_1.txt
report_2.txt
notes.txt

Combining * and []:

List all image files (both .jpg and .png):

ls *.{jpg,png}    # using brace expansion
ls *.[jp]*        # matches .jpg, .png, .jpeg etc.

Combining ? and []:

List only single-digit photo files:

ls photo_[0-9].jpg   # matches photo_1.jpg and photo_2.jpg only

Combining all three:

Match any report file with a single digit:

ls *_[0-9].txt     # matches report_1.txt and report_2.txt

Match files that start with any letter followed by specific characters:

ls [pr]*_?.txt    # starts with p or r, then anything, then underscore, one char, .txt

Using wildcards with other commands:

Move all single-digit jpg photos into a folder:

mv photo_[0-9].jpg archive/

Delete all .log files from any subfolder:

rm */*.log
challenge icon

Challenge

Easy

Use a combination of wildcards to list only the .jpg photo files that have a single digit number — so photo_1.jpg and photo_2.jpg but not photo_10.jpg or any .png files.

Hint: Combine the fixed text photo_, a bracket range for single digits, and the .jpg extension into one pattern.

Cheat sheet

Wildcards can be combined together to create precise file matching patterns by mixing *, ?, and [].

Combining * and []:

ls *.{jpg,png}    # using brace expansion for multiple extensions
ls *.[jp]*        # matches .jpg, .png, .jpeg etc.

Combining ? and []:

ls photo_[0-9].jpg   # matches single-digit photo files only

Combining all three wildcards:

ls *_[0-9].txt     # matches files ending with underscore, single digit, and .txt
ls [pr]*_?.txt     # starts with p or r, then anything, underscore, one char, .txt

Using wildcards with other commands:

mv photo_[0-9].jpg archive/   # move single-digit jpg photos
rm */*.log                     # delete .log files from any subfolder

Try it yourself

Terminal
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals