tee Command in Linux with Examples

By 

Updated on

4 min read

tee command examples in Linux

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 mostly used in combination with other commands through piping.

tee Command Syntax

The syntax for the tee command is as follows:

txt
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 - One or more output files. Each file receives the input data.

How to Use the tee Command

The most basic usage of the tee command is to display the standard output (stdout) of a program and write it to 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.

Terminal
df -h | tee disk_usage.txt
output
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 view the content of the disk_usage.txt file using the cat command .

Here is another simple example that saves kernel messages while still showing them on screen:

Terminal
dmesg | tee /tmp/dmesg.log

Write to Multiple Files

The tee command can also write to multiple files. To do so, specify a list of files separated by space as arguments:

Terminal
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 :

Terminal
command | tee -a file.out

Ignore Interrupt

To ignore interrupts, use the -i (--ignore-interrupts) option. This is useful when stopping the command during execution with Ctrl+C and you want tee to exit gracefully.

Terminal
command | tee -i file.out

Hide the Output

If you do not want tee to write to the standard output, you can redirect it to /dev/null:

Terminal
command | tee file.out >/dev/null

For a quiet append, combine -a with the same redirect:

Terminal
command | tee -a file.out >/dev/null

Using tee with sudo

Suppose 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. The redirection is executed as the unprivileged user.

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

The output will look something like this:

output
bash: /etc/file.conf: Permission denied

Simply prepend sudo before the tee command as shown below:

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

The echo command sends the text through the pipe, and sudo runs tee with permission to write to the protected file.

Use -a to append and avoid truncating an existing file.

Using tee in conjunction with sudo allows you to write to files owned by other users.

If you are writing to multiple files with sudo tee, each destination must be writable by root or the current user.

Quick Reference

For a printable quick reference, see the tee cheatsheet .

TaskCommand
Write output to file and terminalcommand | tee file.txt
Append to a filecommand | tee -a file.txt
Write to multiple filescommand | tee file1.txt file2.txt
Suppress terminal outputcommand | tee file.txt >/dev/null
Write to a root-owned fileecho "text" | sudo tee -a /etc/file.conf

Conclusion

The tee command reads from standard input and writes to standard output and one or more files simultaneously. It also works with binary data. For other ways to redirect output to files, see how to append to a file in Bash .

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page