How to use the Linux Tee Command

Introduction

Before we begin talking about the Linux Tee Command. First, let's understand - What is Linux Tee Command?

The tee command in Linux allows you to display and write output simultaneously.

It mainly reads from the standard input and then writes to both standard outputs and one or more files at the same time.

By using the tee command, you can capture, store, and display command outputs or redirect them to multiple destinations.

In this tutorial, you will use the Linux Tee Command. We will also address few FAQs related to Linux Tee Command.

Step 1 - The tee Command Syntax

1) The syntax for the tee command is below:

tee [OPTIONS] [FILE]

2) Options:

  • -a (--append) - It means not to overwrite the files, instead append to the given files.
  • -i (--ignore-interrupts) - To ignore the interrupt signals.
  • Using the tee --help. It will enable you to view all available options.
  • FILE_NAMES - One or more files to which the output data is written.

Step 2 - Using the tee Command

1) tee command is used to display the standard output (stdout) of a program to files.

2) In the below example, you are using df command. It enables to get information about the amount of available disk space on the file system. Further, the output is piped to the tee command. It will display the output to the terminal and will also write the same information to the file disk_usage.txt:

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 will be able to view the content of the disk_usage.txt file using the cat command.

Step 3 - Writing to Multiple File

1) The tee command will also write to the multiple files. So, you should specify a list of files separated by space as arguments:

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

Step 4 - Appending to File

1) The tee command by default overwrites the specified file. To avoid that, use -a (--append) option to append the output to the file:

command | tee -a file.out

Step 5 - Ignore the Interrupt

1) To ignore interrupts, you will need to use -i (–ignore-interrupt). This is useful to stop the command during execution with CTRL+C and want tee to exit gracefully.

command | tee -i file.out

Step 6 - Hiding the Output

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

command | tee file.out >/dev/null

Step 7 - Using tee in Conjunction with the sudo

1) If you want to write to a file owned by root as a sudo user. The below command will fail, as the redirection of output is not performed by sudo. The redirection gets to execute as the unprivileged user:

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

The output will look like below:

Output

bash: /etc/file.conf: Permission denied

2) Prepend the sudo before tee command as below:

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

The tee command will receive the output of the echo command. It will then elevate to sudo permissions and then write to the file.

3) When you use tee in conjunction with sudo, it allows you to write to files owned by other users.

FAQ's on how to use Linux tee command

How do I use the tee command? 

The basic syntax is command | tee [OPTION]... [FILE].... Use -a to append to existing files and > to overwrite files.

How can I display command output and save it to a file simultaneously? 

Use command | tee file.txt to display the output on the terminal and save it to the specified file.

Can I use tee to write to standard error (stderr)?

 No, tee can only write to standard output (stdout) and files, not to standard error (stderr).

Can I use tee to split the output into multiple directions? 

Yes, you can use multiple instances of tee in a command pipeline to split the output into different directions or files.

How can I write to a file while still displaying the output on the screen? 

You can redirect the output to a file and then use tee to display the output on the screen: command > file.txt | tee -a file.txt.

Is it possible to use tee with compression tools like gzip or bzip2?

Yes, you can combine tee with compression tools like gzip or bzip2 to compress and store command outputs simultaneously: command | tee file.txt | gzip > file.txt.gz.

Is there a Windows equivalent of the tee command? 

The tee command is not built into Windows Command Prompt (CMD). However, you can achieve similar functionality using third-party tools like Cygwin or PowerShell.

Conclusion

We hope this detailed guide helped you to use the Linux Tee Command.

If you have any queries, please leave a comment below and, we’ll be happy to respond to them for sure.