Quick take: The tee command reads from input and writes to both the screen and one or more files. command | tee log.txt shows output and saves it; add -a to append. It is the trick for writing privileged files with sudo in a pipeline.
Introduction
The tee command splits a stream — like a T-junction in plumbing — sending it to both standard output and a file at the same time. This lets you watch a command's output while also saving it, and it solves the classic problem of writing to a root-owned file from within a pipeline.
Syntax
The basic syntax of the tee command is:
command | tee [OPTIONS] FILE...Common Options and Parameters
The most useful options and parameters for the tee command:
| Option | Description |
|---|---|
| (default) | Write input to stdout and overwrite the named files. |
| -a | Append to the files instead of overwriting. |
| -i | Ignore interrupt signals. |
| FILE1 FILE2 | Write to multiple files at once. |
Practical Examples
Real tee commands you can run today:
# Show output and save it
ls -l | tee listing.txt
# Append instead of overwrite
echo 'new line' | tee -a log.txt
# Write to a root-owned file via sudo
echo 'net.ipv4.ip_forward=1' | sudo tee -a /etc/sysctl.conf
# Save to several files at once
command | tee out1.txt out2.txt
# Log a build while watching it
make 2>&1 | tee build.logTips and Best Practices
echo ... | sudo tee fileis the correct way to write a privileged file in a pipeline —sudo echo ... > filefails because the shell opens the file as your user.- Use
-ato append; without it, tee overwrites the file. - Combine with
2>&1to capture both output and errors:command 2>&1 | tee log.
Final Thoughts
tee lets you watch and save output at the same time, and it is the standard solution for writing root-owned files inside a pipeline with sudo tee. Remember -a to append and 2>&1 to capture errors too. It is a small command that solves two everyday problems elegantly.
FAQ: tee Command in Linux
What does the tee command do?+
tee reads from standard input and writes it to both the screen and one or more files simultaneously, so you can see a command's output and save it at the same time.
How do I append with tee instead of overwriting?+
Use the -a flag: command | tee -a file.txt adds the output to the end of the file rather than replacing its contents.
How do I write to a protected file in a pipeline?+
Use sudo tee: echo 'text' | sudo tee -a /etc/file. This works because tee runs under sudo, whereas sudo echo > file fails since the shell opens the file as your normal user.
How do I capture both output and errors with tee?+
Redirect stderr into stdout first: command 2>&1 | tee log.txt records both normal output and error messages while still showing them on screen.
Can tee write to multiple files at once?+
Yes. List several filenames: command | tee a.txt b.txt c.txt writes the same output to all of them.
Need help with Linux servers or infrastructure?
Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.
Hire Me for Support