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.

Narendra K
I'm an experienced and passionate software engineer with in-depth experience in Linux, Distributed systems, DevOps, and Cloud. My expertise lies in back-end web development, and the main languages in my tech stack are Java, Spring, Python, and Go. I’m a lifelong learner and an open-source enthusiast.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

3 thoughts on “How to Use ‘tee’ Command in Linux [8 Useful Examples]”

  1. Hello @LV,

    In the 7th example, we have used the combination of the Vim and Shell commands.

    In this example:

    :w is 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 :w command 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 -i option. First, let’s use the tee command without any arguments:

    [tecmint@tecmint]$ tee
    

    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+C key 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 -i option. In that case, we have to use the ctrl+D key combination to stop the command execution:

    [tecmint@tecmint]$ tee -i
    

    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.

    Reply
  2. 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.

    Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.