$_ CLI In Depth
Learning progress 0%
lesson streams redirection pipes

Output Redirection: >, >> & noclobber

5 min read Module 7: Streams, Redirection & Pipes Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:04

Redirection happens before the command

When bash sees command > file, it does not hand the file name to the command. The shell opens the file itself, in the child process that will run the command, and makes the opened file the new standard output. Only then does the program start. The program receives a file descriptor that already points at the destination, and it has no idea a redirection took place.

This ordering is the source of the most common surprise. The file is opened for writing with truncation, so its old contents are discarded as soon as the shell processes the redirection. If the command then fails, reads its own input from that same file, or prints nothing, the damage is already done.

BASH3 lines
printf 'keep me\n' > data.txt
cat data.txt > data.txt
cat data.txt

The second line truncates data.txt before cat reads it, so the final cat prints nothing. The first cat received an empty file from the shell, and the original text is gone. This is why reading and writing the same file in one command is almost always a mistake.

Appending with >>

The operator >> opens the file for appending. Existing content is kept, and new output is written after the last byte. Use it for logs and for collecting results from several commands into one file.

BASH3 lines
echo first > log.txt
echo second >> log.txt
cat log.txt

The output lists first and then second. Had the second line used a single >, only second would remain, which is a common accident when a script is meant to accumulate output.

Creating an empty file

A redirection with no command creates or truncates a file. The line > file alone is valid, and it is the standard way to reset a log in a script. Because the shell opens the file with truncation, the same mechanism that destroys data is also the reliable way to create an empty file that already exists or does not exist yet.

BASH2 lines
> empty.txt
wc -c empty.txt

The byte count is 0. Whether empty.txt existed before or not, it now holds nothing.

Protecting files with noclobber

Bash has an option that makes > refuse to overwrite an existing regular file. It is called noclobber, and you turn it on with set -o noclobber. When it is active, a plain > to an existing file fails with an error, and you must use >| to override it deliberately. Appending with >> is still allowed, because it does not discard anything.

BASH5 lines
set -o noclobber
echo one > guarded.txt
echo two > guarded.txt; echo "status=$?"
echo two >| guarded.txt
set +o noclobber

The second write fails with a message that the file exists and the status is 1, so guarded.txt still contains only one. The >| form writes two, and the final command turns the option off again. Many people enable noclobber in their interactive shells as a safety net against typing > where they meant >>.

Warning

noclobber protects against accidental overwrites from the shell, but it does not stop a program from truncating a file it opens itself. A script that needs safety should check for the file or write to a temporary name first.

Redirecting several commands at once

Putting a redirection after a group of commands in braces applies it to the whole group. The shell opens the file once, before the first command in the group runs, and every command writes into the same open file. This is cleaner than repeating the redirection on each line, and it avoids the truncation trap that a second > would cause.

{ echo header; echo body; } > report.txt
cat report.txt

The file holds both lines in order. The braces group must end with a semicolon or a newline before the closing brace, which is why the example has a semicolon after body. Groups are also how you redirect a whole block of a script that prints many lines.

Writing a file safely

A safe pattern for generating a file is to write to a temporary name in the same directory, then rename it into place. The rename is atomic on the same file system, so readers see either the old complete file or the new complete file, never a half-written one. The shell itself does not do this for you, so it is a habit you build in scripts.

BASH2 lines
printf 'report\n' > report.tmp && mv report.tmp report.txt
cat report.txt

The output is report. The && makes the move happen only if the write succeeded, so a failed write leaves the previous report in place.

Redirecting into a directory or a missing path

A redirection needs a place to write. If the directory in the path does not exist, the shell reports an error and does not run the command. The command itself is never started, which is useful to know when you see no output at all.

BASH1 lines
echo hi > nodir/out.txt; echo "status=$?"

The shell prints a message about the missing directory, and the status is 1. Nothing is created, and echo never ran. A failed redirection is reported before the command starts, so reading the error message first saves confusion.

Redirections change the file descriptor table

Visualizer

Redirections are applied left to right, before the command runs. >file 2>&1 and 2>&1 >file look alike, but they send stderr to different places.

1 / 4
Before the command
0/dev/pts/0
1/dev/pts/0
2/dev/pts/0
$ ls /nope >out.txt 2>&1

Bash starts with fds 0, 1 and 2 all pointing at the terminal. Redirections change this table before ls runs, and ls inherits whatever the table says.

ls /nope >out.txt 2>&1; echo "status $?"; cat out.txt
Terminal lab · 2 steps · real Linux in your browser

Append, then protect

Use >> to append and noclobber to block accidental overwrites.

01
Append a second line
Write one line with >, then add a second line with >>: ``bash echo one > log.txt echo two >> log.txt ``
Show hint for step 1
Only the first write should use a single >.
02
Refuse to overwrite
Turn on noclobber, write guard.txt, then try to overwrite it. Capture the error from the refused write in refused.txt: ``bash set -o noclobber echo one > guard.txt { echo two > guard.txt; } 2> refused.txt `` The file guard.txt must still contain only one.
Show hint for step 2
Wrap the second write in braces so that the 2> applies to the refused redirection.
Check yourself Question 1 of 4
Concept Check What append means recall

What does the operator >> do to an existing file?

Recap & Key Takeaways

The shell opens the redirection target before the command starts, and > truncates it immediately, even if the command later fails.
Use >> to append, and > alone to create or empty a file.
set -o noclobber makes > refuse to overwrite an existing file, and >| overrides it deliberately.
Write to a temporary file and move it into place when a reader must never see a partial file.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue