Bird
Raised Fist0
Linux CLIscripting~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which wildcard pattern matches all files with any extension starting with log in their name?
easy
A. log* only matches files named exactly 'log'
B. log?
C. log[0-9]
D. log*

Solution

  1. Step 1: Understand the wildcard *

    The * matches zero or more characters, so log* matches any file starting with 'log' followed by anything or nothing.
  2. 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 about log*.
  3. Final Answer:

    log* -> Option D
  4. Quick Check:

    Wildcard * matches any characters [OK]
Hint: Use * to match any number of characters [OK]
Common Mistakes:
  • Confusing * with ? wildcard
  • Thinking * matches only one character
  • Misunderstanding [] as matching multiple characters
2. Which of the following is the correct pattern to match files named data1, data2, but not data10?
easy
A. data?
B. data*
C. data[1-9]
D. data??

Solution

  1. Step 1: Understand the pattern data[1-9]

    This matches 'data' followed by exactly one digit from 1 to 9, so it matches 'data1' and 'data2' but not 'data10'.
  2. Step 2: Check other patterns

    data? matches 'data' plus any one character (including letters), data* matches 'data' plus any characters (including 'data10'), and data?? matches 'data' plus exactly two characters (so it matches 'data10').
  3. Final Answer:

    data[1-9] -> Option C
  4. Quick Check:

    Square brackets match one character from set [OK]
Hint: Use [ ] to match one character from a range [OK]
Common Mistakes:
  • Using * which matches too many files
  • Using ? which matches any single character, not range
  • Confusing number of characters matched
3. Given files: file1.txt, file2.txt, fileA.txt, fileB.txt, what files does the pattern file?.txt match?
medium
A. file1.txt, file2.txt
B. file1.txt, file2.txt, fileA.txt, fileB.txt
C. fileA.txt, fileB.txt
D. file.txt

Solution

  1. Step 1: Understand the pattern file?.txt

    The ? matches exactly one character, so it matches any file named 'file' plus one character plus '.txt'.
  2. 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.
  3. Final Answer:

    file1.txt, file2.txt, fileA.txt, fileB.txt -> Option B
  4. Quick Check:

    ? matches exactly one character [OK]
Hint: Use ? to match exactly one character [OK]
Common Mistakes:
  • Thinking ? matches zero or multiple characters
  • Confusing ? with *
  • Ignoring file extension in pattern
4. You want to list files named report1, report2, and report3 only. Which pattern is incorrect?
medium
A. report*
B. report?
C. report[1-3]
D. report[123]

Solution

  1. Step 1: Analyze pattern report*

    This matches 'report' plus any characters, so it matches more than just 'report1', 'report2', and 'report3'. It includes 'report10', 'report_final', etc.
  2. Step 2: Check other patterns

    report[1-3] and report[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.
  3. Final Answer:

    report* -> Option A
  4. Quick Check:

    * matches too many files [OK]
Hint: Use [] to limit characters, * matches too many [OK]
Common Mistakes:
  • Using * when exact matches needed
  • Assuming ? limits to digits only
  • Not realizing [] matches one character only
5. You have files: img01.png, img02.png, img10.png, img20.png. Which pattern matches only files with numbers from 01 to 09?
hard
A. img0[1-9].png
B. img[0-9][1-9].png
C. img??.png
D. img[01-09].png

Solution

  1. Step 1: Understand pattern img0[1-9].png

    This matches 'img0' plus one digit from 1 to 9, then '.png'. So it matches 'img01.png' to 'img09.png'.
  2. Step 2: Check other patterns

    img[0-9][1-9].png matches any two digits, including 'img10.png' and 'img20.png'. img??.png matches any two characters after 'img', so all files. img[01-09].png is invalid syntax because ranges like '01-09' are not valid inside [].
  3. Final Answer:

    img0[1-9].png -> Option A
  4. Quick Check:

    Use 0 then [1-9] for numbers 01-09 [OK]
Hint: Use fixed digit then range in [] for number ranges [OK]
Common Mistakes:
  • Using invalid range syntax inside []
  • Using ?? which matches any two characters
  • Not fixing leading zero for 01-09
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard