Tee Command in Linux

Learn via video courses
Topics Covered

Overview

tee command in Linux is a utility that allows you to redirect the output of a command to multiple destinations simultaneously. This means that the output can be written to a file and also displayed on the console at the same time. It is a simple yet powerful command that can save time and improve efficiency when working with Linux systems.

Introduction to tee Command in Linux

The tee command in Linux is used to read from standard input and write to standard output and files simultaneously. It takes its name from the letter "T" shape that it creates, with input flowing from one end and output flowing to both the console and files at the other end. The tee command is commonly used in shell scripts and terminal commands to redirect output to multiple locations.

Linux tee Command Syntax

The syntax for the tee command in Linux is straightforward. The basic syntax is as follows:

Here, "options" are the various options that can be used with the tee command, and "file" is the name of the file(s) to which the output will be written.

tee Command Options

The tee command in Linux has various options that can be used to modify its behavior. Some of the most commonly used options are as follows:

OptionsDescription
-aBy default, tee overwrites the contents of the output file(s). The -a option is used to append the output to the end of the file(s) instead of overwriting them.
-iBy default pressing CTRL+C sends an interrupt signal which terminates any running command. The -i option is used to prevent the tee command from being interrupted.
-pThe -p option is used to diagnose errors related to writing to nonpipes
--versionThe --version option will display the version information of the tee utility and exit.
--helpThe --help option is used to display a brief help message that describes the command's usage and available options.

How to Use Tee Command in Linux

The tee command in Linux is a powerful tool that allows us to redirect the output of a command to multiple files, as well as to the standard output. It can be used in multiple ways, and here are some examples of how to use the tee command for various purposes-

Append to Given File

In this example, we will understand how we can append the output of a command to a given file. Example

Output Append

To append the output of a command to an existing file the -a option of the tee command is used. In the above example, the output of the "echo" command is appended to a text file named fruit.txt.

Write to Multiple Files

To write the output of a command to multiple files, the file names can be listed one after another after the tee command. Example

Output write

In the above example, the tee command is used to redirect the output of the echo command to the files, fruits.txt and shakes.txt.

Hide the Output

If you want to redirect the output of a command to a file without having the output on the screen, /dev/null can be used. Example

Output Hide

In the above example, the output is hidden using the /dev/null device, but the output of echo is written in the fruits.txt file.

Redirect Output of One Command to Another

While using the tee command, we can also redirect the output to another command using pipes. Example

Output Redirect Output

In the above example, the cat command is used with the tee command. The output of the cat command is redirected to the tail command which in this case, displays the number of lines specified. The output of the cat command is also written in the shakes.txt file.

Ignore Interrupts

The tee command can be used with the -i option to ensure that it exits properly even if the previous command is interrupted. Example

Output Ignore Interrupts

In the above example, the tee command is writing output in the output.txt file from the ping command and completes it successfully even after ping is interrupted with CTRL+C.

Using tee with sudo

To write in a root-owned file using the tee command, we can use the sudo command before tee. Example

Output In the above example, the tee command is used to write to a root-owned file named hell-world.txt using the sudo command.

Using tee in Vim Editor

We can use the tee command in the Vim editor with the help of the :! command. vim editor

Here, we are editing a file named shakes.txt, and with the help of the tee command, we will write the contents of the file to the output.txt file. Example

Output Using tee in Vim Editor

In the above example, with the help of the cat command, we can see that the content of shakes.txt has been written to output.txt successfully.

Conclusion

  • The tee command in Linux is a powerful tool that allows users to read from standard input and write to standard output and files simultaneously.
  • By default, tee overwrites the output file, but it can also be used to append to an existing file.
  • tee can also be used in conjunction with sudo to write output to a file that requires elevated privileges.
  • Finally, the tee can be used in the Vim editor to write to multiple files simultaneously.