Linux tee Command Tutorial

The Linux tee command is used to process the standard input and copy the input data into another output or a file. The tee command is generally used with other commands in order to save their input into a file and also redirect into the other commands for processing.

tee Command Syntax

The Linux tee command has the following syntax. The tee command standard input feed by a previous command output by using pipe | in general.

tee OPTION FILE
  • OPTION is used to specify different behaviors for the tee command. This is optional.
  • FILE is the file where the tee command input is written. This is required. Also, multiple file names can be used by separating them with spaces.

Print tee Command Help

Even tee is a very simple command we may need to list helpful information about it. The –help option is used to display help information of the tee command.

tee --help
Print tee Command Help

Write Command Output Into A File

The most popular and basic usage of the tee command is writing the previous command output into a file. The previous command output stream is redirected into the tee command input stream. The tee commands both print this input and also writes it into the specified file.

ls -l  | tee file.txt

Write Command Output Into Multiple Files

The tee command can be used to put a command output into multiple files with the same output. We will just provide the file names by separating them with spaces.

ls -l  | tee file.txt data.txt list.txt

Append Command Output Into A File

Another useful feature of the tee command is appending the input into the existing file. This does not overwrite the file output into the existing. The -a option is used to append tee command input into the specified file.

ls -l  | tee -a file.txt

Alternatively the input can be appended into multiple files like below.

ls -l  | tee -a file.txt data.txt list.txt

Delete Command Output or Do Not Display

The tee can be used to delete a specific command output by redirecting into the /dev/null device.

ls -l  | tee /dev/null

Ignore Interrupts

While working with the tee command the interrupt signals or SIGINT can be ignored by using the -i option. This prevents user CTRL+C keyboard shortcut.

ls -l  | tee -i list.txt

Chained Tee Commands

The tee command can be used multiple times by chaining multiple tee commands. This has the same effect by providing multiple files into single tee command.

ls -l  | tee file.txt | tee data.txt | tee list.txt
READ  Linux $PATH Variable Tutorial

Leave a Comment