When you run basic commands on the terminal the output is usually printed to the terminal i.e standard out. But what if you could also save the output in a file as well as print it out to standard out? This is what the tee command does. The Linux tee command reads from stdin ( standard input ) and then writes to stdout ( standard output ) as well as to a file or multiple files.
Basic Syntax of the tee command
The tee command takes the following syntax.
$ command | tee [ options ] file(s)
Let’s now sample a few example usages of the tee command.
Basic use of tee command
Suppose we want to print the memory and swap usage using the free command and save the output in a file called memory_usage.txt. The tee command would be invoked as follows.
$ free -h | tee memory_usage.txt
The tee command reads from the command, saves the output to the memory_usage.txt file, and then prints it to standard out.
To confirm the output was saved to the memory_usage.txt file run the cat command as shown.
$ cat memory_usage.txt
Write the output to multiple files
Additionally, you can save the output to multiple files as shown
$ command | tee [ options ] file1 file2 …
In the command below, the string “Hey, Welcome to Linux” is saved in the two text files: file1.txt and file2.txt
$ echo Hey, Welcome to Linux | tee file1.txt file2.txt
Append content to a file
Usually, the tee command overwrites a file, and this is not always desirable as it can erase existing data that is crucial. Thankfully, you can use the -a option to append text to a file. Let’s test this out.
First, we will write the output of the uptime command to the stats.txt file as shown.
$ uptime | tee stats.txt
Use the cat command to verify this.
$ cat stats.txt
Next, we will append the output of the free -h command which prints our memory and swap usage to the file.
$ free -h | tee -a stats.txt
Yet again, verify the contents of the stats.txt file. This time around, the file will bear the output of the two commands as indicated in the screenshot below. This is because we appended the output of the free -h command to the stats.txt file and therefore the existing text was not affected.
Suppress the output of the tee command
If you don’t wish to have the output of the tee command printed to standard out, you can redirect it to /dev/null which is a special device that discards information is fed to it.
Take the example below where we are printing the output of the df -Th command to the text file but suppressing the output on the terminal.
$ df -Th | tee disk_usage.txt >/dev/null
Let tee command ignore interrupts
Sometimes, you might want to stop a command that is continuously running. When that happens, you might consider having the tee command exit gracefully even after the interruption of the program. To accomplish this, use the -i or –ignore-interrupts option provided in the syntax shown.
$ command | tee -i filename
The ping command below continuously sends ping requests to Google’s DNS ( 18.104.22.168 ). We have interrupted the command after 4 successive ping requests. For the tee command to exit gracefully, invoke the -i option.
$ ping 22.214.171.124 | tee -i ping_stats.txt
Without the -i option, the output and summary of the statistics would not be printed.
Use tee command with sudo
When modifying files owned by the root user or a different login user, simply using the tee command without invoking the sudo command will yield an error.
In the example below, we are creating a new repository called anydesk-stable.list for AnyDesk application in the /etc/apt/sources.list.d path which is a reserve for the root user.
$ echo "deb http://deb.anydesk.com/ all main" | tee /etc/apt/sources.list.d/anydesk-stable.list
As expected, we have run into a ‘permissions denied’ error because we don’t have the permissions to create or modify a file in that path.
The solution is to precede tee with the sudo command as shown.
$ echo "deb http://deb.anydesk.com/ all main" | sudo tee /etc/apt/sources.list.d/anydesk-stable.list
On this occasion, the command is a success after placing sudo before tee.
Get help with tee command
For more command options and assistance in using the tee command, run the command below.
$ tee --help
Additionally, explore the man pages as shown
$ man tee
To check the version, run:
$ tee --version
This is all about the Linux tee command. Notably, the command reads from standard in ( stdin ) and thereafter writes to standard out ( stdout ) and file(s).