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 FILEs, do not overwrite existing content.

-i, --ignore-interrupts
    Ignore interrupt signals (like Ctrl+C), allowing the command to continue running.

DESCRIPTION

tee is a fundamental command-line utility that reads standard input and simultaneously writes it to standard output and to one or more files. This unique capability makes it invaluable for tasks where you need to both view the output of a command on your screen and capture it in a file for later analysis, logging, or further processing. It acts like a "T" junction in a pipe, diverting the flow of data. By default, tee will overwrite the content of the specified files. However, it can be instructed to append to existing files, which is crucial for maintaining logs or accumulating data over time. Its primary use case is within pipelines, allowing intermediate results to be saved without interrupting the flow of data to subsequent commands. tee also handles signals gracefully, making it robust for long-running operations.

CAVEATS

By default, tee overwrites files. Care must be taken to use the -a option when appending is desired to avoid unintended data loss.
If the disk runs out of space or permissions are insufficient, tee may fail to write to files. While it can print errors, the default behavior might not halt the pipeline unless explicitly configured.
For very large inputs, tee's performance might be limited by disk write speeds or file system overhead.

COMMON USAGE EXAMPLES

command | tee output.log
This command runs `command`, displays its output on the screen, and saves it to `output.log`, overwriting the file if it exists.

command | tee -a session.log
Similar to the above, but appends the output to `session.log` instead of overwriting it.

Capturing intermediate pipeline output:
command1 | tee intermediate.txt | command2
The output of `command1` is saved to `intermediate.txt` and also piped as input to `command2`.

ERROR HANDLING

The --output-error option (specific to GNU tee) allows specifying behavior upon write errors (e.g., disk full, permissions denied). Options include warn (default), warn-nopipe, exit, and exit-nopipe, providing more control over pipeline termination when errors occur during file writes.

HISTORY

The tee command has been a standard utility in Unix-like operating systems since their early development. Its design reflects the Unix philosophy of small, specialized tools that can be combined in pipelines to perform complex tasks. It is part of the GNU Core Utilities, ensuring its availability and consistent behavior across most modern Linux distributions. Its enduring presence underscores its fundamental utility in command-line operations and shell scripting.

SEE ALSO

cat(1), script(1), xargs(1)

Copied to clipboard