What if you could save any command's output instantly without lifting a finger?
Why stdout redirection (>, >>) in Linux CLI? - Purpose & Use Cases
Imagine you run a command in the terminal that shows a long list of information, like all files in a folder. You want to save this list to a file to look at later. Without redirection, you have to copy the text manually from the screen and paste it into a file.
Copying text manually is slow and easy to mess up. You might miss some lines or accidentally copy extra spaces. If the output is very long, scrolling back to find it is frustrating. Also, doing this repeatedly wastes time and can cause errors.
Using stdout redirection with > or >> sends the output of a command directly into a file. This means you don't have to copy anything by hand. The first symbol > creates or replaces a file, while >> adds to the file without deleting what's already there.
ls -l
# Then copy output manually to a filels -l > filelist.txt ls -l >> filelist.txt
You can quickly save command results to files for later use, making your work faster and more reliable.
When checking server logs, you can redirect error messages to a file to review them later instead of watching them scroll by on the screen.
Manual copying of command output is slow and error-prone.
Stdout redirection sends output directly to files automatically.
> overwrites files, >> appends to files.
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
