0
0
Linux CLIscripting~10 mins

File globbing (wildcards *, ?, []) in Linux CLI - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - File globbing (wildcards *, ?, [])
Start: User inputs pattern
Shell reads pattern
Match *: any chars
Match ?: single char
Match [
List matching files
Output matched filenames
End
The shell reads the pattern, matches files using wildcards (*, ?, []), then lists all matching filenames.
Execution Sample
Linux CLI
ls *.txt
ls file?.sh
ls data[0-9].csv
These commands list files matching patterns with *, ?, and [] wildcards.
Execution Table
StepCommandPatternFiles in folderMatched FilesOutput
1ls *.txt*.txtfile1.txt, file2.txt, fileA.sh, data1.csvfile1.txt, file2.txtfile1.txt file2.txt
2ls file?.shfile?.shfile1.txt, file2.txt, fileA.sh, fileB.sh, file10.shfileA.sh, fileB.shfileA.sh fileB.sh
3ls data[0-9].csvdata[0-9].csvdata1.csv, data2.csv, dataA.csvdata1.csv, data2.csvdata1.csv data2.csv
4ls data[!0-9].csvdata[!0-9].csvdata1.csv, data2.csv, dataA.csvdataA.csvdataA.csv
5ls *.md*.mdfile1.txt, README.mdREADME.mdREADME.md
6ls file?.shfile?.shfile1.txt, file2.txt, fileA.sh, fileB.sh, file10.shfileA.sh, fileB.shfileA.sh fileB.sh
7ls file??.shfile??.shfile1.txt, file2.txt, fileA.sh, fileB.sh, file10.shfile10.shfile10.sh
8ls *.xyz*.xyzfile1.txt, file2.txt
💡 No more commands to process; last pattern matches no files, so output is empty.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7After 8
Pattern*.txtfile?.shdata[0-9].csvdata[!0-9].csv*.mdfile?.shfile??.sh*.xyz
Matched Filesfile1.txt,file2.txtfileA.sh,fileB.shdata1.csv,data2.csvdataA.csvREADME.mdfileA.sh,fileB.shfile10.sh
Key Moments - 3 Insights
Why does 'file?.sh' match 'fileA.sh' but not 'file10.sh'?
'file?.sh' matches exactly one character in place of '?'. 'fileA.sh' has one character 'A' after 'file', but 'file10.sh' has two characters '10', so it does not match. See execution_table rows 2 and 6.
What does the pattern 'data[0-9].csv' mean?
It matches files starting with 'data', followed by exactly one digit from 0 to 9, then '.csv'. So 'data1.csv' matches but 'dataA.csv' does not. See execution_table row 3.
What happens if no files match the pattern?
The command outputs nothing (empty). For example, '*.xyz' matches no files in the folder, so output is empty. See execution_table row 8.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what files does 'ls file?.sh' match at step 2?
Afile10.sh
Bfile1.txt, file2.txt
CfileA.sh, fileB.sh
DfileA.sh, file10.sh
💡 Hint
Check the 'Matched Files' column at step 2 in the execution_table.
At which step does the pattern match no files?
AStep 5
BStep 8
CStep 3
DStep 1
💡 Hint
Look for empty 'Matched Files' in the execution_table.
If we change 'file?.sh' to 'file??.sh', which file matches?
Afile10.sh
BfileB.sh
CfileA.sh
Dfile1.txt
💡 Hint
Check step 7 in execution_table for 'file??.sh' pattern.
Concept Snapshot
File globbing uses wildcards to match filenames:
* matches any number of characters
? matches exactly one character
[] matches any one character inside brackets
Patterns help list files easily in commands like ls.
Full Transcript
File globbing is how the shell matches filenames using wildcards. The star (*) matches any number of characters, question mark (?) matches exactly one character, and square brackets ([]) match any one character from a set. For example, '*.txt' matches all files ending with .txt. The shell reads the pattern, finds matching files, and lists them. If no files match, the output is empty. Patterns like 'file?.sh' match files with exactly one character after 'file' and before '.sh'. This helps quickly find files without typing full names.