tee Command in Linux



The tee command in Linux reads from the standard input and writes to both standard output and one or more files simultaneously. It is primarily used with the pipe (|) operator. It can be useful for viewing command output in the terminal while also saving it to a file.

Table of Contents

Here is a comprehensive guide to the options available with the tee command in Linux −

Syntax of the tee Command

The syntax of the tee command in Linux is as follows −

[command] | tee [option…] [file…]

In the above syntax, the [command] field is used to specify the command whose output will be directed. The [option...] argument is used to specify one or more options to change the command's behavior. The [file...] is used to specify the file to save the standard output.

tee Command Options

Common options for the tee command are listed below −

Short Option Long Option Description
-a --append Append to the specified files (no overwrite)
-i --ignore-interrupts Ignore interrupt signals
-p Use a more appropriate mode with pipes
--output-error[= MODE ] Define behavior on write errors
--help Show help message and exit
--version Show version information and exit

Examples od tee Command in Linux

This section demonstrates how to use the tee command in Linux with examples −

Displaying and Saving the Output to a File

To display and save the output to a file, use the tee command in the following way −

ls -l | tee file.txt
tee Command in Linux1

To verify, check the contents of the file.txt −

cat file.txt
tee Command in Linux2

Displaying and Saving the Output to Multiple Files

To display and save the output to multiple files, use the following command −

ls -l | tee file1.txt file2.txt

Appending to a File

By default, the tee command overwrites the file. To append the command output to a file instead of overwriting, use the -a or --append option −

echo "New Data" | tee -a file.txt
tee Command in Linux3

Appending to a File with Elevated Permissions

To write or append the file to another file becomes troublesome if the file requires elevated permissions. In that case, sudo can be used with the tee command −

echo "time=20" | sudo tee -a /etc/protected.txt

Error Handling

To set how tee should behave when it encounters a write error, use the --output-error[=MODE] option in the tee command −

echo "time=20" | tee --output-error=exit /etc/protected.txt

If the current user lacks permission to write to /etc/protected.txt, the tee command will exit immediately without writing to any other files.

The following command prints time=20 and attempts to write it to /etc/protected.txt, warning on write errors without stopping the command.

echo "time=20" | tee --output-error=warn /etc/protected.txt

The available modes are listed below −

Mode Description
warn (default) Print a warning, but continue writing to other files.
exit Exit immediately if any write fails.
warn-nopipe Warn only for files, not for broken pipes.
exit-nopipe Exit only for file write errors, not broken pipes.

Ignoring Interrupts

To ignore the interrupt signals like SIGINT (normally sent when pressing Ctrl+C), use the -i or --ignore-interrupts option −

yes "hello..." | tee -i file.txt

This command continuously writes "hello..." to file.txt and displays it in the terminal. If Ctrl+C is pressed, tee ignores the interrupt due to -i, although the yes command may still stop unless it is also protected.

Displaying Usage Help

To display the usage help, use the --help option −

tee --help

Conclusion

The tee command in Linux allows simultaneous viewing and saving of a command's output by writing it to standard output and one or more files. It is often used with a pipe (|) and supports several options such as appending output, ignoring interrupts, and handling write errors.

Useful in scripting and system administration, the tee command helps capture output for logging or configuration updates while keeping it visible in the terminal.

Advertisements