LinuxCommandLibrary

tee

Duplicate standard output to file and screen

TLDR

Copy stdin to each file, and also to stdout

$ echo "example" | tee [path/to/file]
copy

Append to the given files, do not overwrite
$ echo "example" | tee [[-a|--append]] [path/to/file]
copy

Print stdin to the terminal, and also pipe it into another program for further processing
$ echo "example" | tee [/dev/tty] | [xargs printf "[%s]"]
copy

Create a directory called "example", count the number of characters in "example" and write "example" to the terminal
$ echo "example" | tee >(xargs mkdir) >(wc [[-c|--bytes]])
copy

SYNOPSIS

tee [OPTION]... [FILE]...

PARAMETERS

-a, --append
    Append to the given FILE(s), do not overwrite.

-i, --ignore-interrupts
    Ignore interrupt signals (SIGINT).

-p
    Diagnose errors writing to non-pipe files.

--output-error[=MODE]
    Set behavior upon an output error. MODE can be 'warn', 'exit', or 'ignore'. The default is 'exit'.
For information on MODE, see info coreutils 'tee invocation'.

--version
    Output version information and exit.

--help
    Display help and exit.

FILE...
    The file(s) to write to, in addition to standard output.

DESCRIPTION

The tee command reads from standard input and writes to standard output and one or more files simultaneously. It essentially 'splits' the input stream, sending a copy to the terminal and another to the specified file(s). This is useful for capturing the output of a command for later analysis or logging, while still allowing the user to see the output in real-time. The command is particularly handy in pipelines where you want to preserve the intermediate output of a stage without disrupting the flow of data. By default, tee overwrites the specified file(s). Options exist to append to the file, ignore interrupts, and handle buffering.

CAVEATS

If multiple files are specified, tee attempts to write to all of them. Failure to write to one file will not necessarily prevent writing to the others. Disk space limitations on file systems can lead to unexpected output truncations.

USAGE EXAMPLES

1. Capturing output to a file and displaying it on the screen:
ls -l | tee output.txt

2. Appending output to an existing file:
command | tee -a logfile.txt

3. Ignoring interrupt signals:
long_running_command | tee -i output.log

HISTORY

The tee command has been a standard utility in Unix-like operating systems since the early days. Its primary purpose has always been to capture and duplicate output streams. The name 'tee' is an analogy to a T-shaped pipe fitting, which splits a single flow into two separate flows. Over time, standard options like '-a' for appending and '-i' for ignoring interrupts have been added to enhance its functionality.

SEE ALSO

cat(1), sed(1), grep(1), awk(1)

Copied to clipboard