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 to the other commands for processing.

tee Command Syntax

The Linux tee command has the following syntax. The tee command standard input is fed 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 to 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 the user CTRL+C keyboard shortcuts.

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

sudo tee Command

Another popular use case for the tee command is using it with the sudo command. The sudo command is used to provide root privileges to the other commands temporarily. A regular user can not read a file if permissions are not specified properly, but the root user can read every file in the Linux system. We can use the sudo command in order to read every file in the system and provide the tee command. In the following example, we read a system file by using the sudo tee command.

sudo tee /var/log/system

By using the sudo command we prevent file read permission errors.

Bash and Other Shells with tee Command

The tee commands are mostly used with the bash shell but as it is a Linux command we can use the tee command with other shells like zsh, csh etc. In the following example, we run the tee command for the bash, zsh or csh.

tee /home/ismail/log

Leave a Comment