Lesson 34: tee Command
In this lesson, you'll learn how to use the tee command to read from standard input and write to standard output and files simultaneously in Linux.
The tee command reads from the standard input stream and writes it to the standard output stream and files.
tee Command Syntax
The syntax of the tee command is similar to other Linux commands. At a high level, it is divided into two groups - OPTIONS and FILES:
$ tee [OPTIONS] [FILE1] [FILE2] [FILE3] ...
In the above syntax, both OPTIONS and FILES are optional parameters.
tee Command Options
| Option | Description |
|---|---|
-a |
Append the output to the file instead of overwriting it |
-i |
Ignore the interrupt signal (SIGINT) |
1. Save Output to a File
As discussed previously, the tee command sends output to the standard output as well as the file.
To understand this, first let's use the echo command to display the text on the standard output stream:
$ echo "tecmint.com"
tecmint.com
Now, let's use the tee command to write the output to the output.txt file:
$ echo "tecmint.com" | tee output.txt
tecmint.com
Finally, view the contents of the output.txt file using the cat command.
$ cat output.txt
tecmint.com
In the above output, we can see that the tee command sends output to the file as well as the standard output stream.
2. Append Output to a File
By default, the tee command overwrites the output files. However, we can avoid this by enabling the append mode, which appends the output at the end of the file:
$ echo "tecmint.com" | tee -a output.txt
$ cat output.txt
tecmint.com
tecmint.com
In this example, we have used the -a option to enable the append mode.