stdout redirection (>, >>) in Linux CLI - Time & Space Complexity
We want to understand how the time taken by commands changes when we redirect output to files using > or >>.
How does writing output affect the overall execution time as output size grows?
Analyze the time complexity of the following commands that redirect output.
echo "Hello World" > file.txt
cat largefile.txt >> file.txt
The first command writes a small message to a file, overwriting it. The second appends a large file's content to another file.
Look for repeated actions that take time.
- Primary operation: Writing each byte of output to the file.
- How many times: Once for each byte of output produced by the command.
As the amount of output grows, the time to write it grows too.
| Input Size (bytes) | Approx. Operations (writes) |
|---|---|
| 10 | 10 |
| 1000 | 1000 |
| 1000000 | 1000000 |
Pattern observation: The time grows roughly in direct proportion to the output size.
Time Complexity: O(n)
This means the time to redirect output grows linearly with the amount of data being written.
[X] Wrong: "Redirecting output is instant and does not affect command speed."
[OK] Correct: Writing to a file takes time proportional to how much data is written, so large outputs slow down the command.
Understanding how output redirection affects performance helps you write efficient scripts and troubleshoot slow commands.
What if we redirected output to /dev/null instead of a file? How would the time complexity change?
Practice
echo "Hello" > file.txt do in Linux?Solution
Step 1: Understand the > operator
The > operator redirects output to a file, replacing its content.Step 2: Analyze the command effect
echo "Hello" outputs "Hello" and > saves it to file.txt, overwriting old content.Final Answer:
It writes "Hello" to file.txt, replacing any existing content. -> Option AQuick Check:
> means overwrite file [OK]
- Confusing > with >> and thinking it appends
- Believing output only shows on screen
- Assuming file is deleted
Solution
Step 1: Identify append operator
The >> operator appends output to the end of a file without deleting existing content.Step 2: Match command with append operator
Only echo "Log entry" >> log.txt uses >> correctly to append "Log entry" to log.txt.Final Answer:
echo "Log entry" >> log.txt -> Option DQuick Check:
>> means append output [OK]
- Using > which overwrites instead of appending
- Using < which is input redirection, not output
- Using invalid >! operator
echo "First" > data.txt echo "Second" >> data.txt echo "Third" > data.txt
Solution
Step 1: Analyze first command
echo "First" > data.txt writes "First" to data.txt, replacing any content.Step 2: Analyze second command
echo "Second" >> data.txt appends "Second" after "First".Step 3: Analyze third command
echo "Third" > data.txt overwrites entire file with "Third" only.Final Answer:
Third -> Option AQuick Check:
Last > overwrites file content [OK]
- Thinking >> always appends without overwrite
- Assuming all lines remain after last >
- Confusing ">" and ">>" effects
date command to a file named times.log. Which command has an error and why?A) date >> times.log B) date > times.log C) date >>> times.log D) date >> times.log
Solution
Step 1: Check each redirection operator
A uses >> (append), B uses > (overwrite), C uses >>> (invalid), D uses >> (valid).Step 2: Identify invalid syntax
Operator >>> is not valid in Linux shell, causing syntax error.Final Answer:
Syntax error due to invalid operator >>> -> Option CQuick Check:
>> is invalid operator [OK]
- Using >>> instead of >>
- Thinking no space after >> is error
- Confusing > and >> effects
error.log. You want to keep all previous errors and add new ones each time the script runs. Which command should you use inside the script to save the error message "Error occurred" to the file without deleting old logs?Solution
Step 1: Understand logging requirement
We want to keep old logs and add new errors, so appending is needed.Step 2: Choose correct redirection operator
> overwrites file, >> appends to file. So >> is correct to keep old content.Final Answer:
echo "Error occurred" >> error.log -> Option BQuick Check:
Use >> to append logs without deleting [OK]
- Using > which erases old logs
- Using < which is input redirection
- Using invalid >! operator
Ready to prove it?
Pass the test and this pattern moves unknown
