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.txtCombining * 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 onlyCombining all three:
Match any report file with a single digit:
ls *_[0-9].txt # matches report_1.txt and report_2.txtMatch files that start with any letter followed by specific characters:
ls [pr]*_?.txt # starts with p or r, then anything, then underscore, one char, .txtUsing 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 */*.logChallenge
EasyUse 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.jpgextension 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 onlyCombining 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, .txtUsing wildcards with other commands:
mv photo_[0-9].jpg archive/ # move single-digit jpg photos
rm */*.log # delete .log files from any subfolderTry it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Directories
Create A DirectoryCopy A DirectoryMove And Rename A DirectoryDelete A DirectoryRecap - Directory Operations7File Content
Head And TailWord CountSort CommandUnique CommandGrep BasicsGrep With FlagsRecap - Text Detective2Navigation
Print Working DirectoryList FilesChange DirectoryAbsolute vs Relative PathsHome And Root DirectoryRecap - Find Your Way8Redirection
Standard OutputOverwrite To A FileAppend To A FileStandard InputStandard ErrorRecap - Log Builder6Wildcards And Patterns
The Star WildcardThe Question Mark WildcardBracket WildcardsCombining WildcardsRecap - Selective Operations9Piping
What Is A PipeChaining Two CommandsChaining Multiple CommandsPipe With GrepRecap - Data Pipeline