Read in 3 minutes

last updated 

Linux Tee Command with Examples

The tee command reads from the standard input and writes to both standard output and one or more files at the same time. Tee is most commonly used in combination with other commands through piping.

In this tutorial, we’ll cover the basics of using the tee command.

Tee Command Syntax

Before going into how to use the tee command, let’s start by reviewing the basic syntax:

tee [OPTIONS] [FILE]
  • OPTIONS:
    • -a (--append) - Do not overwrite the files instead append to the given files.
    • -i (--ignore-interrupts) - Ignore interrupt signals.
    • Use tee --help to view all available options.
  • FILE_NAMES - One or more files. Each of which the output data being written to.

How to use Linux tee command

The most basic usage of the tee command is to display the standard output (stdout) of a program and write it in a file.

In the following example, we are using the df command to get information about the amount of available disk space on the file system. The output is piped to the tee command, which displays the output to the terminal and writes the same information to the file disk_usage.txt.

df -h | tee disk_usage.txt
Filesystem      Size  Used Avail Use% Mounted on
dev             7.8G     0  7.8G   0% /dev
run             7.9G  1.8M  7.9G   1% /run
/dev/nvme0n1p3  212G  159G   43G  79% /
tmpfs           7.9G  357M  7.5G   5% /dev/shm
tmpfs           7.9G     0  7.9G   0% /sys/fs/cgroup
tmpfs           7.9G   15M  7.9G   1% /tmp
/dev/nvme0n1p1  511M  107M  405M  21% /boot
/dev/sda1       459G  165G  271G  38% /data
tmpfs           1.6G   16K  1.6G   1% /run/user/120

You can verify the content of the disk_usage.txt file using the cat command.

Write to Multiple File

The tee command can also write to multiple files. To do so simply specify a list of files separated by space after the tee command:

command | tee file1.out file2.out file3.out

Append to File

By default the tee command will overwrite the specified file. Use the -a (--append) option to append the output to the file:

command | tee -a file.out

Ignore Interrupt

To ignores interrupts use the `-i (--ignore-interrupts) option. This is useful for example if you interrupt the command during execution with CTRL+C and you want tee to exit gracefully.

command | tee -i file.out

Hide the Output

If you don’t want tee to write to the standard output (display output on the screen), you just need to redirect it to /dev/null:

command | tee file.out >/dev/null

Using tee in Conjunction with sudo

Let’s say you want to write to a file that is owned by root as a sudo user. The following command will fail because the redirection of the output is not performed by sudo, it will happen as the unprivileged user.

sudo echo "newline" > /etc/file.conf

The output will look something like this:

bash: /etc/file.conf: Permission denied

You can use the tee command in conjunction with sudo to write to files owned by other users.

Simply prepend sudo before the tee command as shown below:

echo "newline" | sudo tee -a /etc/file.conf

Tee will receive the output of the echo command, elevate to sudo permissions and write to the file.

Conclusion

By now you should have a good understanding of how to use the Linux tee command.

If you have any question or feedback feel free to leave a comment.