Skip to content
TaeyoungKim.dev

Linux > vs. >>: Why echo Replaced Your File Instead of Appending

LinuxWritten 3 min readTaeyoungKim
LinkedInX

You add one line to a log and discover yesterday's lines are gone. In echo "ready" > run.log, the single > may have replaced the file's contents. What would change with >>?

What is the difference between > and >>?

> redirects a command's standard output to a file. If the file exists, the shell truncates it before writing. >> also redirects output, but appends to the end instead. Both create the file if it does not exist.

SymbolExisting fileTypical use
>Replaces its contentsA freshly generated result
>>Adds output at the endA record built line by line

These symbols are shell redirection, not options specific to echo. They behave the same way when redirecting standard output from printf or another command.

The diagram's “input file” means the file's previous state, not a separate input file. Starting with boot and ready, writing retry with > leaves only retry; >> adds retry after the existing lines.

What remains after writing to the same file?

Use mktemp to create a temporary file instead of modifying an existing file. The variable holds its path.

bash
demo_file=$(mktemp)
echo "boot" > "$demo_file"
echo "ready" >> "$demo_file"
cat "$demo_file"
text
boot
ready

>> appended ready. Now write to that same file with >:

bash
echo "retry" > "$demo_file"
cat "$demo_file"
text
retry

The original lines are gone. Read the file after each write to see the difference directly.

Can > empty a file even when the command fails?

Yes. The shell opens and truncates the output file before running the command. If you try to read a missing input file and redirect the result to an existing output file, the read can fail after that output file has already been emptied.

bash
echo "keep" > "$demo_file"
cat missing-input-for-demo.txt > "$demo_file"
cat "$demo_file"

Assuming missing-input-for-demo.txt does not exist, the second command reports an error and the final cat prints nothing. Try this only with the temporary file, never with an important output file.

Changing > to >> preserves the previous line, but it does not make the failed command succeed. Retries can also append duplicate lines. Avoiding an overwrite and producing a correct result are different concerns.

Which one should you use for logs or results?

> may be right for a report regenerated from scratch. If the old result matters, generate a new file separately and replace the old one only after success. >> fits a simple line-by-line record, but consider duplicate entries on retry and unbounded file growth.

Before choosing a symbol, ask whether the file is a new result each time or a continuing record. If the destination path is uncertain, check the current directory and filename before writing. A single > can empty the wrong file.

Key takeaways

> overwrites an existing file; >> appends to it. The shell handles both, and > can truncate the file even if the command later fails. Avoid writing directly over an important file before verifying that the new result is complete.

Author

TaeyoungKim

Connecting technical foundations with implementation, verification, and production decisions.

#Linux#shell redirection#echo#overwrite#append

Read next