Linuxlab

tee in Linux: write to a file and the screen at once

Updated July 3, 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

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 >>.

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.