File globbing (wildcards *, ?, []) in Linux CLI - Time & Space Complexity
When using file globbing with wildcards, the system searches for matching files. Understanding how the time grows as the number of files increases helps us know how fast or slow this search might be.
We want to know: How does the search time change when there are more files to check?
Analyze the time complexity of the following file globbing command.
ls *.txt
This command lists all files in the current folder that end with .txt using the wildcard *.
- Primary operation: Checking each file name against the pattern.
- How many times: Once for every file in the directory.
As the number of files grows, the system must check more names to find matches.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The number of checks grows directly with the number of files.
Time Complexity: O(n)
This means the time to find matching files grows in a straight line as the number of files increases.
[X] Wrong: "The wildcard search only looks at matching files, so time stays the same no matter how many files there are."
[OK] Correct: The system must check every file name to see if it matches the pattern, so more files mean more checks and more time.
Knowing how file searches scale helps you understand system performance and write scripts that run efficiently, a useful skill in many real-world tasks.
"What if we used a pattern like 'file?.txt' instead of '*.txt'? How would the time complexity change?"
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
