What if you could find or manage hundreds of files with just a few simple symbols?
Why File globbing (wildcards *, ?, []) in Linux CLI? - Purpose & Use Cases
Imagine you have hundreds of files in a folder and you want to find or work with only some of them, like all text files or files starting with 'report'. Doing this by opening each file manually is like searching for a needle in a haystack.
Manually opening or listing files one by one is slow and tiring. You might miss some files or make mistakes typing their names. It's easy to get overwhelmed and waste time.
File globbing lets you use simple symbols like * and ? to quickly match many files at once. It's like using a smart filter that finds exactly what you want without typing every name.
ls report1.txt ls report2.txt ls report3.txt
ls report*.txt
File globbing makes handling groups of files fast and error-free, saving you time and effort.
When cleaning up old photos, you can delete all files ending with .jpg using one command instead of deleting each photo one by one.
Manual file handling is slow and error-prone.
File globbing uses wildcards to match many files easily.
This speeds up tasks like searching, copying, or deleting files.
Practice
log in their name?Solution
Step 1: Understand the wildcard
The**matches zero or more characters, solog*matches any file starting with 'log' followed by anything or nothing.Step 2: Compare other options
log?matches 'log' plus exactly one character,log[0-9]matches 'log' plus one digit, and the last option is incorrect aboutlog*.Final Answer:
log* -> Option DQuick Check:
Wildcard * matches any characters [OK]
- Confusing * with ? wildcard
- Thinking * matches only one character
- Misunderstanding [] as matching multiple characters
data1, data2, but not data10?Solution
Step 1: Understand the pattern
This matches 'data' followed by exactly one digit from 1 to 9, so it matches 'data1' and 'data2' but not 'data10'.data[1-9]Step 2: Check other patterns
data?matches 'data' plus any one character (including letters),data*matches 'data' plus any characters (including 'data10'), anddata??matches 'data' plus exactly two characters (so it matches 'data10').Final Answer:
data[1-9] -> Option CQuick Check:
Square brackets match one character from set [OK]
- Using * which matches too many files
- Using ? which matches any single character, not range
- Confusing number of characters matched
file1.txt, file2.txt, fileA.txt, fileB.txt, what files does the pattern file?.txt match?Solution
Step 1: Understand the pattern
Thefile?.txt?matches exactly one character, so it matches any file named 'file' plus one character plus '.txt'.Step 2: Check which files match
All files with names 'file1.txt', 'file2.txt', 'fileA.txt', and 'fileB.txt' have exactly one character after 'file' before '.txt', so all match.Final Answer:
file1.txt, file2.txt, fileA.txt, fileB.txt -> Option BQuick Check:
? matches exactly one character [OK]
- Thinking ? matches zero or multiple characters
- Confusing ? with *
- Ignoring file extension in pattern
report1, report2, and report3 only. Which pattern is incorrect?Solution
Step 1: Analyze pattern
This matches 'report' plus any characters, so it matches more than just 'report1', 'report2', and 'report3'. It includes 'report10', 'report_final', etc.report*Step 2: Check other patterns
report[1-3]andreport[123]match exactly one character from 1,2,3 after 'report'.report?matches 'report' plus any one character, which includes 'report1', 'report2', 'report3' but also others with one character.Final Answer:
report* -> Option AQuick Check:
* matches too many files [OK]
- Using * when exact matches needed
- Assuming ? limits to digits only
- Not realizing [] matches one character only
img01.png, img02.png, img10.png, img20.png. Which pattern matches only files with numbers from 01 to 09?Solution
Step 1: Understand pattern
This matches 'img0' plus one digit from 1 to 9, then '.png'. So it matches 'img01.png' to 'img09.png'.img0[1-9].pngStep 2: Check other patterns
img[0-9][1-9].pngmatches any two digits, including 'img10.png' and 'img20.png'.img??.pngmatches any two characters after 'img', so all files.img[01-09].pngis invalid syntax because ranges like '01-09' are not valid inside [].Final Answer:
img0[1-9].png -> Option AQuick Check:
Use 0 then [1-9] for numbers 01-09 [OK]
- Using invalid range syntax inside []
- Using ?? which matches any two characters
- Not fixing leading zero for 01-09
Ready to prove it?
Pass the test and this pattern moves unknown
