How to Use ‘tee’ Command in Linux [8 Useful Examples]


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.

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.

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
Save Output to File in Linux
Save Output to File in Linux

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 
Append Output to File in Linux
Append Output to File in Linux

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
Write Output to the Multiple Files in Linux
Write Output to the Multiple Files in Linux

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
Send Output-of Command to Another Command
Send Output-of Command to Another Command

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
Hide Output of File in Linux
Hide Output of File in Linux

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.

Write Output to Privileged File
Write Output to Privileged File

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
Ignore Interrupt Signal
Ignore Interrupt Signal

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.

If You Appreciate What We Do Here On Jassweb, You Should Consider:

Jassweb is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit Jassweb! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?