tee Command Guide for Writing to Files and the Screen
This guide helps you understand and use the tee command, the standard way to split an output stream so it goes both to a file and to the screen.
What is tee, and Why Use It?
The tee command reads standard input and writes it to a file and to standard output at the same time. Its name comes from the T-shaped plumbing fitting that splits one flow of water in two directions — exactly what this command does to a stream of text.
Why Learn tee?
- Watch and record at once: See a long-running command’s output live while keeping a log of it.
- Safe experimentation: Capture the output of a command you’re still testing without losing the screen view.
- The
sudotrick: Redirecting output to a protected file usually needs root; piping throughteelets you run only the write part as root. - It’s redirection you can see: It behaves like
>, except the output stays visible.
teeis the bridge between watching a command work and saving its results. If you’ve ever wishedcommand > filestill printed to the screen, this is the command.
1. Write to a File and Watch It
The basic form sends stdin to a file and to the screen:
ls -l | tee output.txt
- The listing appears on the screen as usual.
- The same text is written to
output.txt.
Any command can feed it:
ping -c 4 example.com | tee ping-log.txt
The screen shows the four pings, and ping-log.txt holds the same output for later.
2. Append Instead of Overwrite (-a)
By default, tee overwrites the file each run, just like >. Add -a to append instead:
date | tee -a log.txt
Run it twice and both entries accumulate in log.txt:
Fri Dec 4 10:15:02 UTC 2026
Fri Dec 4 10:16:44 UTC 2026
This is the mode to use for log files and anything you want to grow over time.
3. Write to Multiple Files at Once
tee accepts several file arguments and writes the same input to all of them:
make 2>&1 | tee build.log build-err.log
- Both files receive identical content.
- Useful when one copy is for humans and another is processed by a later step.
4. The Classic sudo Trick
Redirection like > /etc/hosts runs as your user and fails on protected files, because the shell does the writing — not the command. Piping through tee solves this: only tee needs elevated permission.
echo "127.0.0.1 myhost" | sudo tee -a /etc/hosts
echoruns as you;sudo teeappends with root privileges.- The line also appears on the screen, so you can confirm what was added.
To write a whole replacement file safely:
cat new-config.conf | sudo tee /etc/app/config.conf > /dev/null
> /dev/nullhidestee’s own echo, keeping the screen quiet while the file is written as root.
5. Capturing Errors Too
tee only sees what reaches its stdin, so combine it with redirection first to merge the error stream:
./deploy.sh 2>&1 | tee deploy.log
2>&1redirects stderr into stdout before the pipe.- Both normal output and errors land in
deploy.logand on the screen.
A long-running example — follow a log while saving it:
tail -f /var/log/syslog | tee -a syslog-copy.txt
6. Combining tee with Other Commands
tee slots into any pipeline where you want a durable copy:
ps aux | tee processes.txt | grep ssh
- Everything goes to
processes.txt; onlyssh-related lines continue togrep.
cut -d: -f1 /etc/passwd | tee users.txt | wc -l
- The usernames are saved to
users.txtand counted at the same time.
sort data.txt | uniq | tee cleaned.txt
- The deduplicated result is stored for reuse.
Common Pitfalls
teeoverwrites by default. Forgetting-asilently replaces the file on every run.- The file is written by
tee, not by the first command. Insudo command > file, only the redirection is yours — that’s why thesudo teepattern exists. - Pipeline exit status comes from the last command.
false | tee out.txtreports success becauseteesucceeded; useset -o pipefailin scripts if you need the first command’s status. - Permission errors happen at
tee. If the target file isn’t writable,teefails even when the input command worked fine. - Empty input still creates the file.
true | tee out.txtcreates an emptyout.txt— the file exists either way.
Related Pages
| Page | Relationship |
|---|---|
cat | The usual way to feed a file into tee. |
tail | tail -f | tee is the classic live-logging pattern. |
| Redirection | tee is the visible alternative to > and >>. |
| Pipes | tee splits a stream inside a pipeline. |
wc | Counting what tee just saved. |
sort | tee often stores the input before sorting or processing it. |
tee Cheatsheet
| Flag | Meaning |
|---|---|
| (no flag) | Write input to each file, overwriting it. |
-a | Append to the files instead of overwriting. |
-i | Ignore interrupts; keep writing until stdin closes (GNU). |
-p | Pipe mode: create a FIFO and write input to all writers. |
| FILE… | One or more output files; input is copied to all of them. |
Next: turn text lines into command arguments — xargs.