Almost all power users prefer to use the command line interface while interacting with Linux systems. By default, all Linux commands display their output on the standard output stream. However, sometimes we need to store this output in the files for debugging purposes.
Certainly, we can use the redirection operator to achieve this. However, one of the minor limitations of this method is that it stores the output in the file only.
To overcome this limitation, we can the tee command that reads from the standard input stream and write to the standard output stream and files.
In this practical guide, we will see various examples of the tee command. By the end of this guide, Linux users will be able to use the tee command to fulfill their programming needs.
Table of Contents
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 –
$ tee [OPTIONS] [FILE1] [FILE2] [FILE3] ...
In the above syntax, both
FILES are optional parameters.
1. Save Output to a File in Linux
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"
Now, let’s use the tee command to write the output to the output.txt file:
$ echo "tecmint.com" | tee output.txt
Finally, view the contents of the output.txt file using the cat command.
$ cat output.txt
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 File in Linux
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
In this example, we have used the
-a option to enable the append mode.
3. Write Output to Multiple Files in Linux
Similarly, we can use the tee command to write output to multiple files as shown.
$ echo "tecmint.com" | tee file-1.txt file-2.txt file-3.txt
In this example, we have provided multiple files as command-line arguments. Now, let’s use the head command to display the contents of the files:
$ head -v file-1.txt file-2.txt file-3.txt
4. Send Output of One Command to Another
Additionally, we can also use the tee command with the pipe operator
(|). This method comes in handy when we want to store the output as well as forward it to another command.
$ echo "tecmint.com" | tee example.txt | wc -c $ cat example.txt
In this example, first, we are using the tee command to write the output to the file. Next, we are using the wc command to count the number of characters.
5. Hide Output of File in Linux
In the previous examples, we used the tee command to send the output to the standard output. However, in some rare scenarios, we want to suppress the output. In such cases, we can redirect the output to the /dev/null device:
$ echo "tecmint.com" | tee output.txt > /dev/null $ cat output.txt
6. Write Output to Privileged File
Sometimes, we need to write the output to the privileged file. However, we cannot achieve this using just the tee command. In such scenarios, we can use the combination of the sudo and tee commands.
First, let’s change the ownership of the file using the chown command:
$ sudo chown root:root output.txt
Next, let’s append some text to the output.txt file using the following command:
$ echo "tecmint.com" | sudo tee -a output.txt
Finally, let’s verify that the file has been updated successfully.
7. Edit Privileged File in Linux
In a similar way, we can use the combination of the sudo and tee commands to edit the privileged file. To understand this, first, let’s open a privileged file in a Vim editor and add some text to it:
$ vim output.txt
Now, let’s use the below Vim command to update the file:
:w !sudo tee %
8. Ignore Interrupts Signal (SIGINT)
In Linux, we use the
Ctrl+c key combination to send the SIGINT signal. The default behavior of this signal is to terminate the process. However, we can use the
-i option to ignore the interrupt:
$ echo "tecmint.com" | tee -i example.txt $ cat example.txt
In this guide, we learned about the tee command using some practical examples. Users can refer to this guide while working with Linux systems from the command line interface.
Do you know of any other best example of the tee command in Linux? Let us know your views in the comments below.
3 thoughts on “How to Use ‘tee’ Command in Linux [8 Useful Examples]”
What is a practical application for #8 other than to demonstrate the use of the ‘tee’ command?
In the 7th example, we have used the combination of the Vim and Shell commands.
In this example:
:wis a Vim command which writes buffer contents to the file.
!is also a Vim command and it’s used to execute the shell command.
%is a Vim register that represents the current file. In this case, it will be output.txt.
So in a nutshell, we are using the tee command to redirect the output of the
:wcommand to the current file i.e. output.txt. In this case, the file is owned by the root user. Hence we have to use the combination of the sudo and tee commands.
For the 8th example, let’s understand the command behavior without the
-ioption. First, let’s use the tee command without any arguments:
In this case, the command will read the input from the standard input and will display it on the standard output. At any time, we can use the
ctrl+Ckey combination to terminate the command execution. This happens because the default behavior of the SIGINT signal is to terminate the process.
However, the same key combination will not work if we disable the interrupts using the
-ioption. In that case, we have to use the
ctrl+Dkey combination to stop the command execution:
This option plays an important role when we want to redirect the continuous stream without interruption.
Just like other examples, we can also use the file names with this option.
Thanks for this article, it is helpful. Regarding points 7 and 8 suppose you could explain the need for the action and its syntax a bit further.