0
0
Linux CLIscripting~5 mins

File globbing (wildcards *, ?, []) in Linux CLI - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: File globbing (wildcards *, ?, [])
O(n)
Understanding Time 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?

Scenario Under Consideration

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 *.

Identify Repeating Operations
  • Primary operation: Checking each file name against the pattern.
  • How many times: Once for every file in the directory.
How Execution Grows With Input

As the number of files grows, the system must check more names to find matches.

Input Size (n)Approx. Operations
10About 10 checks
100About 100 checks
1000About 1000 checks

Pattern observation: The number of checks grows directly with the number of files.

Final Time Complexity

Time Complexity: O(n)

This means the time to find matching files grows in a straight line as the number of files increases.

Common Mistake

[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.

Interview Connect

Knowing how file searches scale helps you understand system performance and write scripts that run efficiently, a useful skill in many real-world tasks.

Self-Check

"What if we used a pattern like 'file?.txt' instead of '*.txt'? How would the time complexity change?"