Updated: 30 November 2026 · Applies to: Bash 5.2 on Ubuntu 24.04, also Debian, AlmaLinux and Rocky Linux; notes for dash (/bin/sh)
To save a command's output in a file, put > file after it (this replaces the file) or >> file (this adds to the end). Errors go to a separate stream, so add 2>&1 when you want output and errors in the same file, use > /dev/null to throw output away, and pipe into tee when you want to see the output and save it at the same time.
How output streams work
Every command has three streams, each with a number:
- 0, standard input (stdin): what the command reads.
- 1, standard output (stdout): normal output.
- 2, standard error (stderr): error messages.
A plain > only moves stream 1. That is why error messages still appear on screen when you redirect a command to a file.
Quick reference
command > file: write output to the file, replacing what was in it.command >> file: add output to the end of the file.command 2> file: write only the error messages to the file.command > file 2>&1: output and errors to the same file.command &> file: the same, Bash short form.command >> file 2>&1orcommand &>> file: output and errors added to the end of the file.command > /dev/null 2>&1: discard everything.command 2> /dev/null: hide only the errors.command | tee file: show the output and save it.command < file: read input from a file.
Write output to a file (> and >>)
A single > creates the file, or empties it first if it exists:
echo first > out.txt echo second > out.txt cat out.txt
Output:
second
Use >> to keep the existing lines and add new ones at the end:
echo third >> out.txt cat out.txt
Output:
second third
To stop a stray > from wiping a file by accident, turn on the noclobber option. Bash then refuses to overwrite with bash: out.txt: cannot overwrite existing file, and >| overwrites on purpose:
set -o noclobber
Redirect error messages (2>)
Put the stream number in front of > to redirect errors only:
ls /nonexistent 2> errors.txt cat errors.txt
Output:
ls: cannot access '/nonexistent': No such file or directory
Output and errors in one file (2>&1 and &>)
2>&1 means "send stream 2 to wherever stream 1 goes now". Put it after the file redirection:
ls /etc/hostname /nonexistent > all.txt 2>&1 cat all.txt
Output:
ls: cannot access '/nonexistent': No such file or directory /etc/hostname
The order matters. In command 2>&1 > file, errors are pointed at the screen first (where stdout was at that moment), and only the normal output goes into the file. In our test the error appeared on screen and the file held only /etc/hostname.
In Bash you can write the same thing shorter:
ls /etc/hostname /nonexistent &> both.txt
Use &>> file to append both streams. These short forms are Bash only. In sh scripts and in crontab lines on Debian and Ubuntu (where /bin/sh is dash), command &> file is read as "run the command in the background, then create an empty file". In our dash test the file stayed empty (0 bytes) and the error still appeared on screen. Use > file 2>&1 there instead.
Discard output with /dev/null
/dev/null is a special file that throws away everything written to it. Hide all output, for example in a cron job:
command > /dev/null 2>&1
Hide only the error messages and keep the normal output:
ls /etc/hostname /nonexistent 2> /dev/null
Output:
/etc/hostname
The exit code is not affected: after the command above, echo $? still prints 2, so scripts can still tell that it failed.
For cron jobs it is usually better to keep a log than to discard everything:
/home/alice/backup.sh >> /home/alice/backup.log 2>&1
See the output and save it (tee)
tee copies what it reads to the screen and to a file:
echo hello | tee t.txt
Add -a to append instead of overwrite:
echo again | tee -a t.txt
tee only receives standard output. To save errors too, merge them first:
command 2>&1 | tee output.log
Bash also accepts |& as a short form of 2>&1 |.
A common use is writing to a file that only root may change. sudo echo text > /etc/file fails, because your own shell opens the file before sudo runs. Let tee do the writing as root instead:
echo "text" | sudo tee -a /etc/example.conf > /dev/null
In a pipeline, $? is the exit code of the last command, which is usually tee and usually 0. Run set -o pipefail in scripts so a failing first command is reported: in our test false | tee /dev/null gave exit code 0 without it and 1 with it. For more on tee, see How to Run Commands from Standard Input Using Tee and Xargs in Linux?
Send output to another command (|)
A pipe passes the output of one command to the input of the next instead of a file:
ls /var/log | grep log
And < does the opposite of >: it feeds a file into a command's input.
sort < out.txt
Official documentation: Bash Reference Manual, Redirections.