Viewing and processing text

Command tee

Write the output to a file and leave it in the terminal

Linux · Bash examples · commands run on the selected server

When to use tee

Use tee to inspect command output while saving a copy. Be explicit about whether a report should replace an old file or append to it, and remember that shell privileges apply separately to each pipeline process.

Syntax

tee [OPTIONS] [FILE...]

Square brackets mark optional arguments; an ellipsis means repetition. Replace example file names, user names and server addresses with your own values.

Common options and arguments

-a
Append to file instead of overwriting.
-i
Ignore interrupt signal SIGINT.
--
End option parsing.

Usage examples

Save and show result

df -h | tee ./disk-report.txt

The disk report appears in the terminal and replaces the named report file.

Add entry

printf '%s\n' 'check complete' | tee -a ./checks.log

The check message is appended to the log without replacing existing lines.

Write one output to two files

printf '%s\n' 'ready' | tee ./one.txt ./two.txt

The same stream is written to two separate files and also displayed.

Notes and limitations

  • By default, target files are zeroed; -a saves existing data.
  • tee does not guarantee the success of the entire chain: in Bash, use pipefail if errors from previous commands are important.

Common questions and troubleshooting

Why does sudo before the first command not allow writing?

Each process has its own permissions. Elevating the producer does not elevate tee; grant only the permission needed by the writing process.

Can a report contain only part of a failing command's output?

Yes. A pipeline can create output before an upstream failure. Check relevant exit statuses and capture standard error separately when required.

Further help

Options vary by version and distribution. Check the installed documentation with this command: man tee.