Glossary

tee

tee reads from standard input and writes to both standard output and one or more files. Its name comes from the letter T: the input splits into two paths, like the shape of a T-junction in plumbing. It is the answer whenever you want to save a pipeline's output to a file and see it on screen and pass it on to another command.

command | tee out.txt                 # save and show
command | tee out.txt | wc -l         # save and count
command | tee -a log.txt              # append to file
make 2>&1 | tee build.log             # capture build + errors

A particularly common use is with sudo. You cannot write to a root-owned file with a plain redirection because the redirection is performed by the shell (as the unprivileged user) before sudo runs:

echo "nameserver 1.1.1.1" | sudo tee -a /etc/resolv.conf

Here tee runs as root and can write to the file, while echo runs as you and produces the data. This idiom is ubiquitous in Linux tutorials that need to modify system files from the command line.

Related terms: Pipe, stdout, xargs, Redirection

Discussed in:

Also defined in: Textbook of Linux