9 Tee Examples of Commands in Linux


Linux Tee command is a command line instrument, it reads from the usual enter and write the outcome to straightforward output and information on the similar time.In different phrases, we are able to say, tee command in Linux used for hitting two birds with one stone: studying from normal enter and printing the outcome on a file and to straightforward output on the similar time. What can we imply by this? On this information, we shed extra gentle on Linux tee command and use a couple of examples to reveal its utilization.

Tee Command Syntax

The tee command syntax is kind of easy and takes the next format:

$ tee OPTIONS filename

Listed here are a few of the choices that you need to use with tee command:

In tee command’s syntax, filename refers to a number of information.

With that in thoughts let’s try a couple of examples on how the command is used.

Instance 1) Primary utilization of tee command

As described earlier, the primary perform of the tee command is to show the output of a command (stdout) and reserve it in a file. Within the instance beneath, the command we’re inspecting the block units in our system and piping the outcomes to tee command which show the output to the terminal whereas concurrently saving it on a brand new file referred to as block_devices.txt

$ lsblk | tee block_devices.txt

Be happy to look at the contents of the block_devices.txt file utilizing the cat command as proven:

$ cat block_devices.txt

Instance 2) Save command output to a number of information utilizing tee

Moreover, you possibly can write a command’s output to a number of space-separated information as proven within the syntax beneath.

$ command | tee file1 file2 file3 . . .

Within the following instance, now we have invoked the hostnamectl command to print the hostname of our system amongst different particulars and save the usual output to 2 information file1.txt, and file2.txt

$ hostnamectl | tee file1.txt file2.txt

As soon as once more, you possibly can verify the existence of the output within the two information utilizing the cat command as proven:

$ cat file1.txt
$ cat file2.txt

Instance 3) Suppress output of tee command

If you wish to cover or suppress tee command from printing the output on the display screen then redirect the output to /dev/null as proven:

$ command | tee file > /dev/null

For instance,

$ df -Th | tee file4.txt > /dev/null

Instance 4) Append output to a file with tee command

By default, tee command overwrites the contents of a file. To append the output and stop the erasure of the present content material use the -a or –append choices.

$ command | tee -a file

Within the second command, as proven, now we have appended the output of date command to file1.txt which already comprises the details about the USB units on the system.

$ date | tee -a file1.txt

Instance 5) Use tee along with sudo command

Suppose that as a sudo person, you wish to write on a file that’s owned by the basis person. Naturally, any elevated operation would require that you just invoke the sudo person earlier than the command.

To attain this, merely prefix the tee command with sudo as proven beneath.

$ echo “ db-01” | sudo tee -a /and so on/hosts/

So, tee receives the output of echo command on the left and elevates this utilizing the sudo command which ultimately appends the output to the file.

Instance 6) Redirect output of 1 command to a different utilizing tee command

Utilizing tee command, we are able to simply redirect the output of 1 command to a different command. Right here output of first command will act as enter for second command. Instance is proven beneath:

$ grep ‘root’ /and so on/passwd | tee /tmp/passwd.tmp | wc -l
$ cat /tmp/passwd.tmp

Instance 7) Save adjustments to a file inside vi editor with tee command

Let’s assume you’re working as non-root person and you make adjustments to root owned file and also you overlook to place sudo In entrance of command and now you wish to save adjustments, instance is demonstrated beneath:

$ vi /and so on/hosts   localhost localhost.localdomain localhost4 localhost4.localdomain4   mail.linuxtechi.com   ns.linuxtechi.com    pxe.linuxtechi.com

Whereas saving the adjustments to the file you’ll get read-only message,


Now to save lots of the adjustments to /and so on/hosts file throughout the vi editor, run

:w !sudo tee %

It can immediate you to enter password of the person, if the person has sudo rights then adjustments shall be saved.


Instance 8) Ignore Interrupt sign whereas utilizing tee command

Utilizing ‘-i’ possibility in tee command can ignore interrupt sign (CTRL-C), instance is proven beneath:

$ ping -c 5 linuxtechi.com  | tee -i /tmp/pingtest.tmp

Instance 9) tee command utilization in shell script

Tee command can also be used regularly in shell scripts, one among widespread instance is listed beneath:

$ vi basic-script.sh
LOGFILE=/tmp/basic-logs-$(date +%dpercentmpercentY)
FLAVOR=$(cat /and so on/*-release  | grep -w ‘NAME=’ | lower -d”=” -f2 | awk ‘{print $1}’| sed ‘s/”//g’)
if [ $FLAVOR == CentOS ];
dmesg | grep -i ‘error’ | tee -a $LOGFILE
grep -i ‘put in’ /var/log/dnf.log | tee -a $LOGFILE
echo ‘do nothing’

As we are able to see in above linux shell script, we’re utilizing tee command to append the outputs of two instructions to a log file provided that the OS is CentOS.


This wraps up in the present day’s matter. Be happy to weigh in. Your suggestions is most welcome. On this information, we coated the Linux tee command.

Learn Additionally: 14 Grep Command Examples in Linux

tee 2>&1,tee vs cat,tee kali linux,tee no such file or directory,tee unix pipe,differentiate cat command from more command,tee to file and stdout,myapp tee mylog1 txt,linux tee stderr,in cut, which two options come together?,linux make tee,command file,grep with the option -n prints __________.,tee command to redirect output,linux tea,wc command in linux,tee overwrite file,grep tee command,bash for loop,push in linux,pushd popd bash,how does pushd and popd work,xargs -n 1,xargs multiple commands,linux xargs,tee command in linux,tee linux,pipes redirection and tee command in linux,tee command windows,aix tee command example

You May Also Like