Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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 sudo trick: Redirecting output to a protected file usually needs root; piping through tee lets you run only the write part as root.
  • It’s redirection you can see: It behaves like >, except the output stays visible.

tee is the bridge between watching a command work and saving its results. If you’ve ever wished command > file still 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
  • echo runs as you; sudo tee appends 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/null hides tee’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>&1 redirects stderr into stdout before the pipe.
  • Both normal output and errors land in deploy.log and 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; only ssh-related lines continue to grep.
cut -d: -f1 /etc/passwd | tee users.txt | wc -l
  • The usernames are saved to users.txt and counted at the same time.
sort data.txt | uniq | tee cleaned.txt
  • The deduplicated result is stored for reuse.

Common Pitfalls

  • tee overwrites by default. Forgetting -a silently replaces the file on every run.
  • The file is written by tee, not by the first command. In sudo command > file, only the redirection is yours — that’s why the sudo tee pattern exists.
  • Pipeline exit status comes from the last command. false | tee out.txt reports success because tee succeeded; use set -o pipefail in scripts if you need the first command’s status.
  • Permission errors happen at tee. If the target file isn’t writable, tee fails even when the input command worked fine.
  • Empty input still creates the file. true | tee out.txt creates an empty out.txt — the file exists either way.

PageRelationship
catThe usual way to feed a file into tee.
tailtail -f | tee is the classic live-logging pattern.
Redirectiontee is the visible alternative to > and >>.
Pipestee splits a stream inside a pipeline.
wcCounting what tee just saved.
sorttee often stores the input before sorting or processing it.

tee Cheatsheet

FlagMeaning
(no flag)Write input to each file, overwriting it.
-aAppend to the files instead of overwriting.
-iIgnore interrupts; keep writing until stdin closes (GNU).
-pPipe 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.