tee

The tee command reads from the standard input and writes to both standard output and one or more files at the same time.

How to Use the Tee Command in Linux - Make Tech Easier

tee is mostly used in combination with other commands through piping.

Introduction to the tee Command - Baeldung on Linux

Syntax

tee [OPTIONS] [FILE]

Copy

  • OPTIONS
    • -a (--append) : Do not overwrite the files instead append to the given files.
    • -i (--ignore-interrupts) : Ignore interrupt signals.
    • Use tee --help to view all available options.
  • FILE One or more files. Each of which the output data is written to.

Usage

Display output and write it in a file

The most basic usage of the tee command is to display the standard output (stdout) of a program and write it in a file.

Example:

$ echo "Hello world!" | tee hello-world.txt
Hello World!
$ cat hello-world.txt
Hello World!

The output is piped to the tee command, which displays the output to the terminal and writes the same information to the file hello-world.txt.

Write to Multiple Files

The tee command can also write to multiple files. To do so, specify a list of files separated by space as arguments:

$ command | tee file1.out file2.out file3.out

Append to File

By default, the tee command will overwrite the specified file. Use the -a (--append) option to append the output to the file :

$ command | tee -a file.out

E.g.

$ echo "Hello world!" | tee hello-world.txt
Hello world!
$ cat hello-world.txt
Hello World!
$ echo "Hi world!" | tee -a hello-world.txt
Hi world!
$ cat hello-world.txt
Hello World!
Hi world!

Ignore Interrupt

To ignore interrupts use the -i (--ignore-interrupts) option. This is useful when stopping the command during execution with CTRL+C and want tee to exit gracefully.

$ command | tee -i file.out

Hide the Output

If you don’t want tee to write to the standard output, you can redirect it to /dev/null:

$ command | tee file.out >/dev/null

Reference