Short answer: tee copies whatever comes in on standard input to the screen and to one or more files at the same time: command | tee file.txt. Add -a to append instead of overwriting, and pipe into sudo tee to write a file that only root may change.
command | tee output.txt # screen and file, file overwritten
command | tee -a output.txt # screen and file, appended
command 2>&1 | tee output.txt # include error messages
command | tee one.txt two.txt # several files at once
command | tee output.txt > /dev/null # file only, nothing on screen
echo "line" | sudo tee -a /etc/some.conf > /dev/null # file owned by roottee is part of GNU coreutils, so it is already on every Debian, Ubuntu and Rocky Linux server. Every output below comes from tee 9.1 (coreutils) and bash 5.2.15 on Debian 12, run in a scratch directory with throwaway files.
Screen and file: tee and tee -a
$ printf "alpha\nbeta\n" | tee out.txt
alpha
beta
$ echo gamma | tee -a out.txt
gamma
$ cat out.txt
alpha
beta
gamma
$ echo delta | tee out.txt
delta
$ cat out.txt
deltatee prints every line and writes it to the file. Without -a, tee empties the file as soon as it starts, which is why the last command left only delta. To watch a log that tee is writing from a second terminal, use tail -f on the file.
| Option | What it does |
|---|---|
-a, --append | Append to the files instead of overwriting them |
-i, --ignore-interrupts | Ignore SIGINT, so Ctrl+C stops the command but tee still writes what the command prints on its way out |
-p | Keep writing to the files when a pipe it writes to closes (the same as --output-error=warn-nopipe) |
--output-error=MODE | warn, warn-nopipe, exit or exit-nopipe: what to do when an output fails |
Several files at once, and a file that fails
$ date -u +%F | tee a.txt b.txt c.txt
2026-11-09
$ wc -l a.txt b.txt c.txt
1 a.txt
1 b.txt
1 c.txt
3 total
$ echo hi | tee nodir/x.txt ok.txt; echo "exit=$?"
tee: nodir/x.txt: No such file or directory
hi
exit=1
$ cat ok.txt
hitee takes any number of files. If one cannot be opened, tee reports it, still writes to the screen and to every other file, and exits with status 1 at the end, so a script notices.
The same applies when a write fails later, for example on a full disk: tee prints a warning, keeps writing to the screen and the other files, and exits with status 1 at the end, while the job itself runs on. If you would rather stop the job than let it run with an incomplete log, use --output-error=exit: tee quits at the first failed write, and the command in front of it is killed by SIGPIPE on its next write (we checked both with /dev/full, a device that fails every write). -i covers Ctrl+C: it sends SIGINT to every process of the pipeline, and with tee -i the log still receives whatever the job prints while it shuts down, such as its cleanup messages. Without -i, tee dies at once and those lines are lost.
Only the file, not the screen
$ echo "only in the file" | tee quiet.txt > /dev/null
$ cat quiet.txt
only in the fileThe > /dev/null belongs to tee's own standard output, the copy that would go to the screen. Writing tee /dev/null does not silence anything: tee treats /dev/null as one more file to write to and still prints every line. The quiet form is the usual tail of a sudo tee line in setup scripts.
Save a copy from the middle of a pipeline
$ sort hosts.txt | tee sorted.txt | uniq -c
3 web1
1 web2
2 web3
$ cat sorted.txt
web1
web1
web1
web2
web3
web3tee in the middle passes everything on unchanged and keeps a copy, which is handy for checking what a long pipeline looks like halfway through. The same trick hashes a file while it is being saved, without reading it twice:
$ cat disk.img | tee copy.img | sha256sum
b51d376156816079... -
$ sha256sum disk.img copy.img
b51d376156816079... disk.img
b51d376156816079... copy.imgFor a download that is curl -fsSL URL | tee file.tar.gz | sha256sum: compare the printed hash with the one the project publishes before you unpack anything.
Feed several commands at once: tee >(...)
$ seq 100000 | tee >(gzip > numbers.gz) >(wc -l > count.txt) | tail -n 1
100000
$ cat count.txt
100000
$ gzip -l numbers.gz
compressed uncompressed ratio uncompressed_name
215157 588895 63.5% numbers>(command) is bash and zsh process substitution: the shell starts the command and hands tee a path that leads to its input, so one stream reaches gzip, wc and tail at once. It does not exist in sh (dash on Debian and Ubuntu), so a script that uses it needs #!/bin/bash. The substituted commands run in the background and may still be writing when the next line of a script starts; bash's wait covers only the last one. We read the two files a second later. A script that needs the results right away should write one file with tee and run the other commands on it afterwards.
Error messages too: 2>&1 | tee and |& tee
$ ls notes.txt missing.txt |& tee both.log
ls: cannot access 'missing.txt': No such file or directory
notes.txt
$ cat both.log
ls: cannot access 'missing.txt': No such file or directory
notes.txtA pipe carries only standard output, so error messages bypass tee unless you merge them first. |& is bash's short form of 2>&1 |. Why the order of redirections matters, and what happens without the merge, is in redirecting stdout and stderr.
Root-owned files: why sudo tee and not sudo echo
sudo echo "203.0.113.10 backup01" >> /etc/hosts # Permission denied
echo "203.0.113.10 backup01" | sudo tee -a /etc/hosts > /dev/null # works
sudo sh -c 'echo "203.0.113.10 backup01" >> /etc/hosts' # also worksA redirection is not an argument of sudo: bash sets it up first, with your own rights, and only then starts sudo, so the first line fails before sudo even runs. In the second line tee is the program that opens the file, and tee is what runs as root. The third line hands the whole command, redirection included, to a root shell, the form the sudo manual itself uses. Forget the -a and /etc/hosts is replaced by that one line. A whole config file written with a heredoc and sudo tee is in the guide to creating files.
Exit status: tee hides the failure
$ grep -c root /nonexistent | tee count.txt
grep: /nonexistent: No such file or directory
$ echo "exit=$? grep=${PIPESTATUS[0]} tee=${PIPESTATUS[1]}"
exit=0 grep=2 tee=0
$ set -o pipefail
$ grep -c root /nonexistent | tee count.txt; echo "exit=$?"
grep: /nonexistent: No such file or directory
exit=2A pipeline returns the exit status of its last command, and tee succeeded, so $? is 0 even though grep failed. ${PIPESTATUS[0]} holds the status of the first command; set -o pipefail makes the whole pipeline fail when any part fails. Put it near the top of every bash script that pipes into tee. The pipefail section of the redirection guide has one more example.
When the reader stops early: tee -p
$ seq 1000000 | tee all.txt | head -n 3
1
2
3
$ echo "${PIPESTATUS[*]}"; wc -l all.txt
141 141 0
1859 all.txt
$ seq 1000000 | tee -p all.txt | head -n 3
1
2
3
$ echo "${PIPESTATUS[*]}"; wc -l all.txt
0 0 0
1000000 all.txthead exits after three lines and closes the pipe. By default tee then dies on the failed write (status 141 is 128 plus signal 13, SIGPIPE, and seq goes the same way once tee is gone), so the file holds only what had been written so far: between 1,859 and 13,456 lines in six runs here, a number that depends on timing. With -p tee ignores the closed pipe, keeps reading and writes all one million lines to the file.
On a VPS or dedicated server, tee is most useful around long jobs you run by hand over SSH, such as an upgrade or a migration: sudo apt full-upgrade 2>&1 | tee -a ~/upgrade.log keeps a record you can read later. tee does not protect the job itself: if the SSH connection drops, the pipeline normally dies with it, so start such jobs inside tmux or screen.
Frequently asked
What does the tee command do in Linux?
tee reads standard input and writes it both to standard output and to every file named on its command line. command | tee file.txt shows the output and saves it at the same time. Without -a the file is overwritten.
How do I append with tee instead of overwriting?
Use tee -a file. Without -a, tee empties the file as soon as it starts, before any data arrives. The same works with sudo: echo line | sudo tee -a /etc/file > /dev/null adds one line to a root-owned file and prints nothing.
Why does $? show 0 after a command piped into tee failed?
A pipeline reports the exit status of its last command, which is tee, and tee succeeded. In bash, ${PIPESTATUS[0]} holds the status of the first command, and set -o pipefail at the top of a script makes the pipeline fail when any part of it fails.
Why is the file written by tee incomplete or empty?
Two common causes. A command after tee, such as head, closed the pipe early and tee died of SIGPIPE, which tee -p avoids. Or the file was also the input of the pipeline and tee emptied it before it was read; write to a new file and rename it afterwards.