Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Viewing and Searching Files

Linux tee command

Reference for the Linux tee command: copy standard input to a file and to standard output, append with -a, write to several files and use sudo tee.

tee reads standard input and writes it to standard output and to one or more files. The name comes from the T-shaped pipe fitting: the data flows on, but a copy is taken on the side. It lets you save the output of a command while still watching it, or in the middle of a pipeline to keep an intermediate result.

Terminal
command | tee [OPTION]... [FILE]...

Try it

Bash Save and show at once

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Inside a pipeline and with stderr

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
tee [-a] [-i] [-p] [--output-error[=MODE]] [FILE]...
Option Description
-a, --append Append to the files instead of overwriting them.
-i, --ignore-interrupts Ignore SIGINT (Ctrl+C), so a saved log is not cut short.
-p Diagnose errors writing to non-pipes (with --output-error).
--output-error[=MODE] Set behavior on write errors: warn, warn-nopipe, exit, exit-nopipe.
FILE Files to write; - is treated as a normal file name in GNU tee.

Returns: 0 on success; non-zero if any file could not be opened or written.

Usage notes

  • Files are truncated at start. Without -a, an existing file is emptied as soon as tee starts, even if no data arrives.
  • Only stdout is piped. cmd | tee log does not capture standard error. Use cmd 2>&1 | tee log, or in Bash cmd |& tee log.
  • sudo tee writes to protected files. echo text > /etc/file fails because the redirection runs as you, before sudo. Use echo text | sudo tee /etc/file > /dev/null, or sudo tee -a to append.
  • Discarding the screen copy with > /dev/null is common when you only want the file or the write-through to a privileged path.
  • Buffering. Output is buffered when it is not a terminal. For live logs through several pipes, a command in the chain may need stdbuf -oL or --line-buffered.
  • Exit status of the pipeline is that of the last command only (unless set -o pipefail), so a failing producer is hidden: false | tee f succeeds.
  • Same file as input and output (cat f | tee f) destroys data. Write to a new file.
  • Process substitution >(cmd) lets one input feed several commands, for example compressing one copy while counting another.