Linux Tee command is a command line tool, it reads from the standard input and write the result to standard output and files at the same time.In other words, we can say, tee command in Linux used for hitting two birds with one stone: reading from standard input and printing the result on a file and to standard output at the same time. What do we mean by this? In this guide, we shed more light on Linux tee command and use a few examples to demonstrate its usage.
Tee Command Syntax
The tee command syntax is quite simple and takes the following format:
$ tee OPTIONS filename
Here are some of the options that you can use with tee command:
In tee command’s syntax, filename refers to one or more files.
With that in mind let’s check out a few examples on how the command is used.
Example 1) Basic usage of tee command
As described earlier, the main function of the tee command is to display the output of a command (stdout) and save it in a file. In the example below, the command we are inspecting the block devices in our system and piping the results to tee command which display the output to the terminal while simultaneously saving it on a new file called block_devices.txt
$ lsblk | tee block_devices.txt
Feel free to examine the contents of the block_devices.txt file using the cat command as shown:
$ cat block_devices.txt
Example 2) Save command output to multiple files using tee
Additionally, you can write a command’s output to several space-separated files as shown in the syntax below.
$ command | tee file1 file2 file3 . . .
In the following example, we have invoked the hostnamectl command to print the hostname of our system among other details and save the standard output to two files file1.txt, and file2.txt
$ hostnamectl | tee file1.txt file2.txt
Once again, you can confirm the existence of the output in the two files using the cat command as shown:
$ cat file1.txt $ cat file2.txt
Example 3) Suppress output of tee command
If you want to hide or suppress tee command from printing the output on the screen then redirect the output to /dev/null as shown:
$ command | tee file > /dev/null
For example,
$ df -Th | tee file4.txt > /dev/null
Example 4) Append output to a file with tee command
By default, tee command overwrites the contents of a file. To append the output and prevent the erasure of the current content, use the -a or –append options.
$ command | tee -a file
In the second command, as shown, we have appended the output of date command to file1.txt which already contains the information about the USB devices on the system.
$ date | tee -a file1.txt
Example 5) Use tee together with sudo command
Suppose that as a sudo user, you want to write on a file that is owned by the root user. Naturally, any elevated operation will require that you invoke the sudo user before the command.
To achieve this, simply prefix the tee command with sudo as shown below.
$ echo "10.200.50.20 db-01" | sudo tee -a /etc/hosts/
So, tee receives the output of echo command on the left and elevates this using the sudo command which eventually appends the output to the file.
Example 6) Redirect output of one command to another using tee command
Using tee command, we can easily redirect the output of one command to another command. Here output of first command will act as input for second command. Example is shown below:
$ grep 'root' /etc/passwd | tee /tmp/passwd.tmp | wc -l 2 $ cat /tmp/passwd.tmp root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin $
Example 7) Save changes to a file within vi editor with tee command
Let’s assume you are working as non-root user and you are making changes to root owned file and you forget to put sudo In front of command and now you want to save changes, example is demonstrated below:
$ vi /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 192.168.1.60 mail.linuxtechi.com 192.168.1.50 ns.linuxtechi.com 192.168.1.40 pxe.linuxtechi.com
While saving the changes to the file you will get read-only message,
Now to save the changes to /etc/hosts file within the vi editor, run
:w !sudo tee %
It will prompt you to enter password of the user, if the user has sudo rights then changes will be saved.
Example 8) Ignore Interrupt signal while using tee command
Using ‘-i’ option in tee command can ignore interrupt signal (CTRL-C), example is shown below:
$ ping -c 5 linuxtechi.com | tee -i /tmp/pingtest.tmp
Example 9) tee command usage in shell script
Tee command is also used frequently in shell scripts, one of common example is listed below:
$ vi basic-script.sh #!/bin/bash LOGFILE=/tmp/basic-logs-$(date +%d%m%Y) FLAVOR=$(cat /etc/*-release | grep -w 'NAME=' | cut -d"=" -f2 | awk '{print $1}'| sed 's/"//g') if [ $FLAVOR == CentOS ]; then dmesg | grep -i 'error' | tee -a $LOGFILE grep -i 'installed' /var/log/dnf.log | tee -a $LOGFILE else echo 'do nothing' fi
As we can see in above linux shell script, we are using tee command to append the outputs of two commands to a log file only if the OS is CentOS.
Conclusion
This wraps up today’s topic. Feel free to weigh in. Your feedback is most welcome. In this guide, we covered the Linux tee command.
Read Also: 14 Grep Command Examples in Linux
Pradeep, Thank you for this excellent and well written guide