0
0
Linux CLIscripting~5 mins

touch (create empty files) in Linux CLI - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: touch (create empty files)
O(n)
Understanding Time Complexity

We want to understand how the time to create files using the touch command changes as we create more files.

How does the work grow when we ask the system to make many empty files?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.

for i in $(seq 1 1000); do
  touch file_$i.txt
 done

This script creates 1000 empty files named file_1.txt to file_1000.txt one by one.

Identify Repeating Operations
  • Primary operation: The touch command inside the loop creates one file each time.
  • How many times: It runs once for each file, so 1000 times in this example.
How Execution Grows With Input

Each new file requires one touch command. So if you double the number of files, you double the work.

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

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

Final Time Complexity

Time Complexity: O(n)

This means the time to create files grows in a straight line with how many files you want to make.

Common Mistake

[X] Wrong: "Creating many files with touch happens instantly no matter how many files."

[OK] Correct: Each file creation takes some time, so more files mean more total time.

Interview Connect

Understanding how commands scale with input size helps you write scripts that run efficiently and predict how long tasks will take.

Self-Check

"What if we used a single command to create all files at once? How would the time complexity change?"