What is the difference between the > and >> redirection operators in Linux?
Learn the difference between > and >> in Linux: how > overwrites a file while >> appends output, plus stderr redirection and noclobber tips.
Expected Interview Answer
The > operator redirects a command's standard output to a file, overwriting (truncating) the file's existing contents, while >> redirects output to a file by appending to the end, preserving whatever was already there.
Both operate on standard output (file descriptor 1) by default; if the target file does not exist, both create it. The key difference is that > truncates the file to zero length before writing, so previous content is lost, whereas >> keeps existing content and adds new output after it. You can redirect other descriptors too, such as 2> for stderr, and the noclobber shell option can protect against accidental overwrites with >.
- > gives a clean file each run, ideal for fresh reports
- >> preserves history, ideal for logs
- Both create the file automatically if missing
- Work with any descriptor, e.g. 2> for errors
- Enable simple, powerful file-based automation
AI Mentor Explanation
Using > is like starting a brand-new scorecard for the innings: the sheet is wiped clean and only today's runs are recorded, erasing anything written before. Using >> is like adding to the season's rolling batting log — each innings is appended beneath the last, so the full run history stays intact while new scores accumulate over time.
Step-by-Step Explanation
Step 1
Write with overwrite
Run echo hello > file.txt; the file now contains only 'hello', replacing any earlier contents.
Step 2
Overwrite again
Run echo world > file.txt; the file now contains only 'world' because > truncated it first.
Step 3
Append instead
Run echo again >> file.txt; 'again' is added on a new line after the existing content.
Step 4
Verify the result
Run cat file.txt to confirm >> kept the previous line and added the new one below it.
Step 5
Redirect other descriptors
Use 2> or 2>> to send standard error to a file, overwriting or appending just like stdout.
What Interviewer Expects
- Knows > truncates and >> appends
- Understands both create the file if it is missing
- Aware both default to standard output (fd 1)
- Can redirect stderr with 2> and 2>>
- Mentions the noclobber option to guard against overwrites
Common Mistakes
- Thinking > appends instead of overwriting
- Believing >> only works on existing files
- Assuming > and >> redirect stderr by default
- Forgetting that > silently destroys existing content
- Confusing redirection with piping (|)
Best Answer (HR Friendly)
“In Linux, > sends a command's output into a file and wipes whatever was there before, while >> adds the output to the end of the file without deleting the existing content. Use > for a fresh file each time and >> when you want to keep building up a log.”
Code Example
# Overwrite: file ends up with just 'first'
echo first > log.txt
echo second > log.txt # replaces 'first'
cat log.txt # -> second
# Append: keep existing content
echo third >> log.txt
cat log.txt # -> second then third
# Append standard error too
myscript 2>> errors.txtFollow-up Questions
- How do you redirect standard error to a file?
- What does 2>&1 do and where should it be placed?
- What is the noclobber option and how does it affect >?
- What is the difference between redirection and a pipe?
- How do you discard output using /dev/null?
MCQ Practice
1. What does the > operator do to an existing target file?
> truncates the file to zero length before writing, so any previous contents are lost and replaced by the new output.
2. Which operator appends output while preserving existing content?
>> opens the file in append mode, adding new output after the existing content instead of overwriting it.
3. If the target file does not exist, what do > and >> do?
Both operators create the file automatically if it does not already exist; they differ only in how they treat existing content.
Flash Cards
What does > do? — Redirects stdout to a file, truncating (overwriting) any existing content; creates the file if missing.
What does >> do? — Redirects stdout to a file in append mode, preserving existing content and adding new output at the end.
Do > and >> create missing files? — Yes, both create the target file automatically if it does not already exist.
How do you append stderr? — Use 2>> file to append standard error (fd 2) to a file, mirroring >> for stdout.