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.
command | tee [OPTION]... [FILE]...
Try it
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.
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
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 asteestarts, even if no data arrives. - Only stdout is piped.
cmd | tee logdoes not capture standard error. Usecmd 2>&1 | tee log, or in Bashcmd |& tee log. sudo teewrites to protected files.echo text > /etc/filefails because the redirection runs as you, beforesudo. Useecho text | sudo tee /etc/file > /dev/null, orsudo tee -ato append.- Discarding the screen copy with
> /dev/nullis 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 -oLor--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 fsucceeds. - 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.