touch (create empty files) in Linux CLI - Time & Space 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?
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.
- Primary operation: The
touchcommand inside the loop creates one file each time. - How many times: It runs once for each file, so 1000 times in this example.
Each new file requires one touch command. So if you double the number of files, you double the work.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 touch commands |
| 100 | 100 touch commands |
| 1000 | 1000 touch commands |
Pattern observation: The number of operations grows directly with the number of files.
Time Complexity: O(n)
This means the time to create files grows in a straight line with how many files you want to make.
[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.
Understanding how commands scale with input size helps you write scripts that run efficiently and predict how long tasks will take.
"What if we used a single command to create all files at once? How would the time complexity change?"
Practice
touch command do in Linux?Solution
Step 1: Understand the purpose of
Thetouchtouchcommand is used to create empty files or update the modification time of existing files.Step 2: Compare with other commands
Deleting files is done withrm, copying withcp, and displaying contents withcat. None of these aretouch.Final Answer:
Creates an empty file or updates the timestamp of an existing file -> Option CQuick Check:
touch= create/update file time [OK]
- Confusing touch with delete or copy commands
- Thinking touch shows file content
- Assuming touch edits file content
notes.txt using touch?Solution
Step 1: Identify the basic syntax of
The basic command to create or update a file istouchtouch filename. Sotouch notes.txtis correct.Step 2: Understand other options
-cprevents creating a file if it doesn't exist,>is shell redirection (not touch syntax), and-rcopies timestamps from another file.Final Answer:
touch notes.txt -> Option AQuick Check:
Basic touch syntax = touch filename [OK]
- Using shell redirection instead of touch syntax
- Adding unnecessary options like -c or -r
- Confusing touch options with other commands
touch file1.txt file2.txt ls -l file1.txt file2.txt
Solution
Step 1: Understand
Thetouchwith multiple filestouchcommand can create multiple files at once by listing them separated by spaces.Step 2: Check
Thels -loutputls -lcommand lists detailed info of the files, showing their size (0 bytes if empty) and timestamps.Final Answer:
Lists details of file1.txt and file2.txt, both created as empty files -> Option DQuick Check:
touch multiple files = creates all listed [OK]
- Thinking touch only creates one file at a time
- Expecting an error with multiple filenames
- Confusing touch with delete or move commands
touch newfile.txt but get an error: touch: cannot touch 'newfile.txt': Permission denied. What is the likely cause?Solution
Step 1: Analyze the error message
The error says "Permission denied" which means the user lacks rights to create or modify files in the directory.Step 2: Check other options
If the file exists, touch updates timestamp without error. Misspelling touch causes command not found, not permission denied. Disk full causes different errors.Final Answer:
You do not have write permission in the directory -> Option AQuick Check:
Permission denied = no write access [OK]
- Assuming file existence causes permission error
- Thinking touch command typo causes permission error
- Ignoring directory permissions
log1.txt, log2.txt, ..., log5.txt quickly using a single command. Which command achieves this?Solution
Step 1: Understand brace expansion
The syntaxlog{1..5}.txtuses brace expansion to create multiple filenames from 1 to 5.Step 2: Evaluate other options
log[1-5].txtmatches single characters in brackets, not ranges. Listing all files manually works but is long.log*.txtmatches existing files, not create new ones.Final Answer:
touch log{1..5}.txt -> Option BQuick Check:
Brace expansion creates multiple files fast [OK]
- Using square brackets instead of braces for ranges
- Typing all filenames manually instead of using expansion
- Using wildcard which matches existing files only
Ready to prove it?
Pass the test and this pattern moves unknown
