Redirecting Output with > and >>
Redirecting output means telling Bash to send a command's standard output to a file instead of the screen. The operator > writes to a file and replaces whatever was in it, and the operator >> adds to the end of a file and keeps what was there. Together they are how you save results, build log files and generate text files from the command line.
The Basic Syntax
command > file
command >> file
The word after the operator is the name of the file. The command itself is written exactly as before, and its options and arguments do not change. The redirection is an extra instruction to the shell that says where file descriptor 1, standard output, should go.
echo "first" > notes.txt
cat notes.txt
Output:
first
The echo command printed nothing on the screen. Its output was written to notes.txt, and cat showed the contents afterward.
How It Works
The important detail is who does the work. The command never sees the redirection. Bash handles it before the command starts, in these steps:
- Bash reads the line and finds the
>operator and the file name. - Bash opens the file for writing. If the file does not exist, it is created. If it does exist, it is emptied (truncated) immediately.
- Bash connects the open file to file descriptor 1 of the new process.
- Bash starts the command, which writes to standard output as usual. The text goes into the file.
Because the file is opened before the command runs, the file exists even if the command prints nothing, and an existing file is emptied even if the command fails. You can see the first step at work when a command lists its own output file:
ls -1 > listing.txt
cat listing.txt
In a folder that held early.txt, fresh.txt, notes.txt and year.txt, the output was:
early.txt
fresh.txt
listing.txt
notes.txt
year.txt
The file listing.txt is in its own listing, because Bash created it before ls started.
Overwriting with >
A single > replaces the contents of the file every time:
echo "first" > notes.txt
echo "second" > notes.txt
cat notes.txt
Output:
second
The text first is gone. Each command with > starts from an empty file. This is the right behavior for a report that should always reflect the latest run, and the wrong behavior for a log that should keep history.
Appending with >>
The >> operator opens the file for appending. The old contents stay, and new output is added at the end:
echo "second" > notes.txt
echo "third" >> notes.txt
echo "fourth" >> notes.txt
cat notes.txt
Output:
second
third
fourth
If the file does not exist, >> creates it, just as > does:
echo "hello" >> fresh.txt
cat fresh.txt
Output:
hello
This makes >> safe to use for a log file that may not exist on the first run.
Saving Command Output
Any command that prints to standard output can be redirected. Some typical uses:
date +%Y > year.txt
ls -1 > listing.txt
printf 'no newline' > nn.txt
printf ' more\n' >> nn.txt
cat nn.txt
Output:
no newline more
The printf command, unlike echo, adds a newline only when you write \n. The first command wrote text with no newline at the end, and the second appended directly after it, so the two pieces joined on one line.
A log file with one line per event is a good use of appending:
echo "$(date +%F) backup done" >> app.log
echo "$(date +%F) cleanup done" >> app.log
cat app.log
Output (the dates are the day you run it; shown here for 21 November 2026):
2026-11-21 backup done
2026-11-21 cleanup done
The $(date +%F) part is command substitution, covered earlier. It inserts the current date in year-month-day form before echo runs.
Emptying a File
A redirection with no command at all creates the file or empties it:
echo "x" > notes.txt
> notes.txt
wc -c < notes.txt
Output:
0
The wc -c command counts bytes, and it reports 0, so the file is empty. Many scripts write the same thing with the colon command, which does nothing and succeeds:
: > notes.txt
Both forms worked in Bash and in Dash. The colon form states the intent more clearly, because a reader sees an explicit command and not just a redirection, and some other shells (Zsh, for example) treat a bare > file differently, so the colon form is the portable choice.
Where the Redirection Can Go
The redirection does not have to be at the end of the line. Bash accepts it before the command too, and the result is the same:
> early.txt echo "written early"
cat early.txt
Output:
written early
Putting the redirection at the end is the usual style and is easier to read. The flexibility matters when you read other people's scripts or when a command is built in several parts.
The Redirection Applies to One Command
A redirection affects only the command it is attached to. The next command on the line, or the next line, prints to the screen again:
echo "one" > only.txt; echo "two"
cat only.txt
Output:
two
one
The two appeared on the screen, and one went into the file.
Redirecting a Group of Commands
To send the output of several commands to one file, wrap them in curly braces or put the redirection after a loop:
for n in 1 2 3; do echo "line $n"; done > loop.txt
cat loop.txt
{ echo "header"; echo "body"; } > group.txt
cat group.txt
Output:
line 1
line 2
line 3
header
body
In the loop example, the file is opened once for the whole loop, so all three lines end up in it. If the redirection were inside the loop as echo "line $n" > loop.txt, each pass would empty the file and only the last line would remain. For loops that add to a file, use >> inside the loop or one > after the done.
Notice the spaces in the braces form: a space after {, and a semicolon (or newline) before }.
Only Standard Output Is Captured
The operator > is short for 1>, which means "redirect file descriptor 1". Standard error is descriptor 2, so it is not affected:
ls nothing.txt > cap.txt
Output:
ls: cannot access 'nothing.txt': No such file or directory
The error message still appeared on the screen, and cap.txt was created but stayed empty (0 bytes). A later lesson in this section shows how to capture standard error. You can write the descriptor number explicitly if you like:
echo "explicit" 1> explicit.txt
cat explicit.txt
Output:
explicit
More Than One Redirection
If you give the same descriptor two destinations, the last one wins, and the earlier file is still created:
echo "both" > one.txt > two.txt
echo "one: $(wc -c < one.txt) two: $(wc -c < two.txt)"
Output:
one: 0 two: 5
The file one.txt was created and left empty. The text both and its newline (5 bytes) went to two.txt. This is rarely useful, but it explains odd results when a line has a stray extra redirection.
When the Redirection Fails
If Bash cannot open the file, it prints an error and does not run the command. The two most common reasons are a missing folder and a target that is a directory:
echo hi > nodir/file.txt
echo "status: $?"
mkdir adir
echo hi > adir
echo "status: $?"
Output:
bash: nodir/file.txt: No such file or directory
status: 1
bash: adir: Is a directory
status: 1
Bash does not create missing folders. Use mkdir -p first when the folder may not exist. The status is 1, and the command, here echo, never ran. The same is true for any other command: after a failed redirection, touch ranflag > nodir/x leaves no ranflag file, because touch was never started.
Permissions cause the same kind of failure. When an ordinary user writes to a file owned by root, Bash reports:
bash: /etc/hostname: Permission denied
The message names the file, not the command, which is a clue that the redirection and not the command failed.
Overwriting Dangers
Because > empties the file before the command runs, reading and writing the same file in one command destroys the data:
printf 'b\na\n' > data.txt
sort data.txt > data.txt
wc -c < data.txt
Output:
0
Bash emptied data.txt first, so sort read an empty file and wrote an empty result. The original text is lost. The same thing happens with cat:
printf 'keep\n' > a.txt
cat a.txt > a.txt
wc -c < a.txt
Output:
0
The safe pattern is to write to a temporary file and then replace the original:
printf 'b\na\n' > data.txt
sort data.txt > sorted.txt && mv sorted.txt data.txt
cat data.txt
Output:
a
b
The && means the mv runs only if sort succeeded. Some commands, such as sort -o data.txt data.txt, have a built-in option for writing to the input file safely, so check the manual of the command first.
Protecting Files with noclobber
Bash has an option called noclobber that makes > refuse to overwrite an existing file. You turn it on with set -o noclobber:
echo "keep me" > precious.txt
set -o noclobber
echo "overwrite" > precious.txt
echo "status: $?"
echo "append ok" >> precious.txt
echo "forced" >| precious.txt
cat precious.txt
set +o noclobber
Output:
bash: precious.txt: cannot overwrite existing file
status: 1
forced
With the option on:
>fails on an existing file with the message shown.>>still works, since it does not destroy anything.>|forces an overwrite when you really mean it.
Run set +o noclobber to turn the protection off again. The option only guards against accidental >, so it does not replace careful commands, but it is a useful safety net in an interactive shell.
A Short Preview of Related Features
The echo ... > file pattern is the simplest way to write a file from a script. Writing several lines at once is easier with a here document, covered later in this section. Capturing errors with 2> and combining both streams with 2>&1 are the topics of the following lessons.