0
0
Linux CLIscripting~5 mins

mkdir (create directories) in Linux CLI - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: mkdir (create directories)
O(n)
Understanding Time Complexity

When creating directories using the mkdir command, it's helpful to understand how the time it takes grows as you create more directories.

We want to know how the work done by mkdir changes when the number of directories increases.

Scenario Under Consideration

Analyze the time complexity of the following code snippet.

for dir in dir1 dir2 dir3 dir4 dir5; do
  mkdir "$dir"
done

This script creates five directories one by one using a loop.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: The mkdir command inside the loop creates one directory per iteration.
  • How many times: The loop runs once for each directory name, so it runs n times if there are n directories.
How Execution Grows With Input

Each directory creation takes roughly the same amount of time, so total time grows as you add more directories.

Input Size (n)Approx. Operations
1010 mkdir commands run
100100 mkdir commands run
10001000 mkdir commands run

Pattern observation: The total work grows directly with the number of directories you create.

Final Time Complexity

Time Complexity: O(n)

This means the time to create directories grows linearly with how many directories you want to make.

Common Mistake

[X] Wrong: "Creating multiple directories with mkdir is always instant no matter how many directories."

[OK] Correct: Each directory creation takes some time, so more directories mean more total time, even if each one is quick.

Interview Connect

Understanding how simple commands like mkdir scale helps you think clearly about scripts and automation tasks in real work.

Self-Check

"What if we used mkdir -p to create nested directories in one command? How would the time complexity change?"