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?"