tee in Linux: write to a file and the screen at once
Updated November 1, 2026
tee writes what passes through a pipe both to a file and to the screen at once.
Syntax
... | tee fileCommon cases
ls -l | tee out.txt # show and save
make 2>&1 | tee build.log # watch the build and record a log
ls | tee a.txt b.txt # to several files at once
echo on | sudo tee /sys/file # write to a root-owned file in a pipe
ls | tee -a out.txt # -a append instead of overwriteSample output
$ printf "alpha\nbeta\ngamma\n" | tee words.txt | wc -l
3
$ cat words.txt
alpha
beta
gammaHow it works: a T-joint in the pipe
The name
teecomes from a plumbing T-joint: the stream comes in and splits two ways. Withoutteeyou have to choose: either> file(output goes to the file, the screen is empty) or watch the screen (nothing is saved).teedoes both at once. A common trick issudo tee: writingsudo command > /filemakes your shell do the redirect, and the shell has no root rights, so it fails; whilecommand | sudo tee /filewrites to the file as root. The-aflag appends, like>>.
Common mistakes
tee sees standard output only. ls /nope | tee log.txt prints ls: cannot access '/nope': No such file or directory on the screen and puts not a single byte into log.txt - and the old content of that file is gone as well, because tee truncates the file when it opens it, the same way > does. To get errors into the log, merge stream 2 into stream 1 before the pipe: command 2>&1 | tee log.txt.
The exit status of a pipeline that ends in tee is the status of tee, and that is almost always 0. false | tee build.log; echo $? prints 0, so a build that failed under tee looks successful and the CI step turns green. set -o pipefail before it gives an honest answer: the same pipeline then returns 1.
When the right side of the pipe stops reading, tee is cut off mid-write. seq 1 100000 | tee all.txt | head -3 prints three lines, head closes, tee gets SIGPIPE and dies: all.txt held 10043 lines out of a hundred thousand, and the next run gives a different number. The file looks normal, it is just short. If you need the whole file, keep head off the end of the pipeline.
In the book
This topic is covered in full in the chapter Streams, redirection, pipelines.
See also
Redirection > and >>, Pipes and filters.
Try it
Open the sandbox: a real throwaway Linux terminal in your browser. Run the commands above by hand - the container is deleted when you leave.