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:

9 Tee Examples of Commands in Linux9 Tee Examples of Commands in Linux

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
9 Tee Examples of Commands in Linux9 Tee Examples of Commands in Linux

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
9 Tee Examples of Commands in Linux9 Tee Examples of Commands in Linux

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
9 Tee Examples of Commands in Linux9 Tee Examples of Commands in Linux

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
9 Tee Examples of Commands in Linux9 Tee Examples of Commands in Linux

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 “10.200.50.20 db-01” | sudo tee -a /and so on/hosts/
9 Tee Examples of Commands in Linux9 Tee Examples of Commands in Linux

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
2
$ cat /tmp/passwd.tmp
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
$

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
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

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

9 Tee Examples of Commands in Linux9 Tee Examples of Commands in Linux

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.

9 Tee Examples of Commands in Linux9 Tee Examples of Commands in Linux9 Tee Examples of Commands in Linux9 Tee Examples of Commands in Linux

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
9 Tee Examples of Commands in Linux9 Tee Examples of Commands in Linux

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
#!/bin/bash
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 ];
then
dmesg | grep -i ‘error’ | tee -a $LOGFILE
grep -i ‘put in’ /var/log/dnf.log | tee -a $LOGFILE
else
echo ‘do nothing’
fi
9 Tee Examples of Commands in Linux9 Tee Examples of Commands in Linux

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.

Conclusion

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

Calculation Number of Days Between Two Dates

Topic: Delphi Language charlene44 wrote: 18/08/2006 at 15h08 calculation of number of…

Top Benefits of Social Media Marketing

We cannot deny the fact that when it comes to marketing, social…

World of Warcraft Classic: Some Players Use Illegal Tools to Stay Connected

Earlier this week, Blizzard released World of Warcraft Classic. The launch was,…

Crash Team Racing Bientt Back on Ps4!

Activision seems not to have finished with the remastering of the Playstation…