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 file

Common 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 overwrite

Sample output

$ printf "alpha\nbeta\ngamma\n" | tee words.txt | wc -l
3
$ cat words.txt
alpha
beta
gamma

How it works: a T-joint in the pipe

The name tee comes from a plumbing T-joint: the stream comes in and splits two ways. Without tee you have to choose: either > file (output goes to the file, the screen is empty) or watch the screen (nothing is saved). tee does both at once. A common trick is sudo tee: writing sudo command > /file makes your shell do the redirect, and the shell has no root rights, so it fails; while command | sudo tee /file writes to the file as root. The -a flag 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.