Linux | tee Command With Examples

The tee command in Linux reads the standard input from the command execution and can pass the output to new files or the other commands. The tee command is used with other commands with the help of the piping character *|*. In this blog, the usage of the tee command in Linux will be explained with the help of some examples. The content covered in this post is:

Let’s start!

What is the Tee Command in Linux?

The tee command is used to read the standard output, save it in the new file, and is also used with the piping with other commands.

To use the tee command, follow the below-mentioned syntax:

$ tee [Options] [Filename]

The usage of this tee command is explained in the next section.

Options

Many options can be used with the tee command, and some of these options are explained below:

OptionsExplanation
-aIt will append the content to the file.
-pIt will find out the errors
-iIt will ignore all the interrupts while executing the command
–helpIt will display the help menu on the screen
–versionIt will display the version of the tee command

All these options can be used in the [Options] as explained in the general syntax of using the tee command.

What is the Usage of the tee Command in Linux?

This section comprises a list of various examples that represent the usage of the tee command in Linux. Let’s dig into them:

Example 1: Saving the Output of the Command in External File

To understand this, we display the content of a text file piping with the tee command:

$ cat testfile1 | tee mynewfile

In the above output, the content of the testfile1 has been displayed with the help of the cat command, but at the same time, the tee command reads and write the output in the new file named, “mynewfile”. To confirm this, we will display the content of the “mynewfile”:

$ cat mynewfile

Example 2: Saving the Input to Multiple Files

To understand it, we will run another command, let’s say, we print something like “Hello” and store it in multiple files using the tee command:

$ echo ‘Hello’ | tee myfile1 myfile2 myfile3

The “Hello” string has been saved in all these files.

Example 3: Hide the Output and Save it in the File Using the Tee command Utility

To explain this, we will run the update command but save its output in the update_file.txt with the tee command:

$ sudo ps | tee ps_file.txt >/dev/null

The information of the processes is not displayed on the screen but stored in the ps_file.txt.

Example 4: Ignore the interrupts Using the Tee Command

We can ignore the interrupts by using the “i” option of the tee command; for example, we will ping google and try to interrupt the ping process:

$ ping google.com | tee -i interrupt_file.txt

The command execution is interrupted, but if we display the interrupt_file.txt, no interrupt will be found:

$ cat interrupt_file.txt

That’s all from this post!

Conclusion

In Linux, the tee command is used to read the standard output of the executed command and passes it through the other command or to an external file. It can be used with various command line utilities to get the output. This blog has helped the readers understand the use of the tee command with the help of examples in Linux.

Over 500 Million Views on Our Blogs and YouTube! We’re inviting 10 businesses to partner with us and grow together Click here to join us ×