Explain the use of the tee command with examples

The tee command in Linux is used to display the output as well as store the output in some files too, The tee command takes the input from the stdin and displays the output of stdout. This saves the times the time of the user. In this article, we will discuss the tee command general syntax and will understand the usage of tee command with the help of some examples.

The general syntax of using the tee command

The general syntax of using the tee command is:

$ tee [options] [file name]

The explanation of the above syntax is simple:

  • Use the tee command to read the stdin, display the stdout, and store it in some file
  • We can use different options, these options are explained in the next section
  • The file name is used to name a file where we want to save the output

The different options which can be used with the tee command are:

OptionsExplanation
-aThis flag is used to append the given files and restrict the overwrite
-iThis flag is used to ignore the signals which are making interruption
-pIt is used to find out the errors which occur during writing to the non-pipes
-helpIt is used to find out the help of using the tee command
-versionTo find out the version of the tee command

To understand the use of the ping command, we will ping google and using its information from stdin, save the output in the file, myfile using the tee command:

$ ping google.com | tee myfile.txt

We will display the contents of myfile.txt using the cat command:

$ cat myfile.txt

We can also display and save the output in multiple files, for example, we will print “Hey! Its LinuxFoss” and save the output in myfile1.txt and myfile2.txt:

$ echo “Hey! Its LinuxFoss” | tee myfile1.txt myfile2.txt

To display the myfile1.txt and myfile2.txt use the commands:

$ cat myfile1.txt
$ cat myfile2.txt

We can also append the input to the files using the “-a” flag:

$ echo “Hey! This is a platform to understand the Linux” | tee -a myfile1.txt

To confirm the results, we will view the myfile.txt using the command:

$ cat myfile1.txt

In the above output, we can understand the line has been added to the file without overwriting the contents of a file, myfile1.txt. To view the version of the tee command use the “–version” flag:

$ tee --version

To know more about the tee command, use the “-help” flag:

$ tee --help

Conclusion

The tee command saves time by taking the stdin, displaying the stdout as well as storing it in some files. In this write-up, we have explained the general syntax of the tee command and also explain the different options which can be used with the tee command. This Linux tool helps us to automatically save the stdout in some files.