9 tee Command Examples in Linux
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 …