Linux tee command with examples

The tee command reads standard input (stdin) and writes the result to standard output (stdout) and one or more files simultaneously.

It basically displays the command output on the terminal and at the same time redirects the output to a file. I often used tee command to append a new line to the root user files.

In this article, we’ll show how to use the tee command on Linux.

The basic syntax for the tee command is:

[command] | tee [options] [filename]

1) Basic usage of tee command

One of the basic uses of the tee command is to display the standard output (stdout) of a command and save it in a file. For instance, we have executed the ‘hostnamectl’ command to print our system’s hostname and other details, then save the standard output to ‘host_info.txt’.

$ hostnamectl | tee host_info.txt

    Static hostname: 2daygeek
          Icon name: computer-laptop
            Chassis: laptop
         Machine ID: 7a138ee71db94e8785d1a4dbe54dde7e
            Boot ID: 34d27941b74941a9a29bc424f66613bc
   Operating System: openSUSE Leap 15.2
        CPE OS Name: cpe:/o:opensuse:leap:15.2
             Kernel: Linux 5.3.18-lp152.75-default
       Architecture: x86-64

The content of the ‘host_info.txt’ file can be viewed using the cat command as follows:

$ cat host_info.txt

2) Write output to multiple files

This can also write output to multiple files. To do so, add a list of files separated by space as arguments.

$ command | tee file1 file2…fileN

3) Append output to a file

By default, the tee command overwrites the contents of a file, each time you run it. To append output, use the '-a or --append' option.

$ command | tee -a file

4) Ignore Interrupts

This option allows tee command to exit gracefully when the command has been interrupted with (CTRL+C) during execution. To do so, use the '-i or --ignore-interrupts' option

$ command | tee -i file

For example: The tee command writes the ping command output properly even after ping is interrupted with 'Ctrl+C':

5) Hide the output

If you don’t want to print the command result to a standard output, use the following format:

$ command | tee file >/dev/null

6) Using tee with sudo

The tee command allows you to write a file owned by root user or others. For example, if you want to append a new line to the below file, you will be getting “permission denied” error because the redirection of the output is not performed by sudo.

$ sudo echo "add a newline" > /etc/sudoers
bash: /etc/sudoers: Permission denied

To do so, use the tee command as follows:

$ echo "add a newline" | sudo tee -a /etc/sudoers

7) Use of tee command in shell script

The tee command can often be used in shell scripts to redirect the output to a file as shown below:

$ vi /tmp/

LOGFILE=/tmp/test-logs-$(date +%d%m%Y)
echo "Append the logs routinely" | tee -a $LOGFILE

Closing Notes

In this guide, we’ve shown you how to use tee command in Linux.

If you have any questions or feedback, feel free to comment below.

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

Leave a Reply

Your email address will not be published. Required fields are marked *