HomeLinux8 'tee' Command Examples and Utilization in Linux

8 ‘tee’ Command Examples and Utilization in Linux


Nearly all energy customers want to make use of the command line interface whereas interacting with Linux programs. By default, all Linux instructions show their output on the usual output stream. Nonetheless, typically we have to retailer this output within the information for debugging functions.

Actually, we are able to use the redirection operator to realize this. Nonetheless, one of many minor limitations of this methodology is that it shops the output within the file solely.

To beat this limitation, we are able to the tee command that reads from the usual enter stream and write to the usual output stream and information.

On this sensible information, we’ll see varied examples of the tee command. By the top of this information, Linux customers will be capable to use the tee command to meet their programming wants.

tee Command Syntax

The syntax of the tee command is just like different Linux instructions. At a excessive stage, it’s divided into two teams – OPTIONS and FILES:

$ tee [OPTIONS] [FILE1] [FILE2] [FILE3] ...

Within the above syntax, each OPTIONS and FILES are non-obligatory parameters.

1. Save Output to a File in Linux

As mentioned beforehand, the tee command sends output to the usual output in addition to the file. To know this, first, let’s use the echo command to show the textual content on the usual output stream:

$ echo "tecmint.com"

Now, let’s use the tee command to put in writing the output to the output.txt file:

$ echo "tecmint.com" | tee output.txt

Lastly, view the contents of the output.txt file utilizing the cat command.

$ cat output.txt
Save Output to File in Linux
Save Output to File in Linux

Within the above output, we are able to see that the tee command sends output to the file in addition to the usual output stream.

2. Append Output to File in Linux

By default, the tee command overwrites the output information. Nonetheless, we are able to keep away from this by enabling the append mode, which appends the output on the finish of the file:

$ echo "tecmint.com" | tee -a output.txt 
$ cat output.txt 
Append Output to File in Linux
Append Output to File in Linux

On this instance, now we have used the -a choice to allow the append mode.

3. Write Output to A number of Recordsdata in Linux

Equally, we are able to use the tee command to put in writing output to a number of information as proven.

$ echo "tecmint.com" | tee file-1.txt file-2.txt file-3.txt

On this instance, now we have offered a number of information as command-line arguments. Now, let’s use the head command to show the contents of the information:

$ head -v file-1.txt file-2.txt file-3.txt
Write Output to the Multiple Files in Linux
Write Output to the A number of Recordsdata in Linux

4. Ship Output of One Command to One other

Moreover, we are able to additionally use the tee command with the pipe operator (|). This methodology is useful after we need to retailer the output in addition to ahead it to a different command.

$ echo "tecmint.com" | tee instance.txt | wc -c
$ cat instance.txt
Send Output-of Command to Another Command
Ship Output-of Command to One other Command

On this instance, first, we’re utilizing the tee command to put in writing the output to the file. Subsequent, we’re utilizing the wc command to depend the variety of characters.

5. Disguise Output of File in Linux

Within the earlier examples, we used the tee command to ship the output to the usual output. Nonetheless, in some uncommon eventualities, we need to suppress the output. In such circumstances, we are able to redirect the output to the /dev/null gadget:

$ echo "tecmint.com" | tee output.txt > /dev/null
$ cat output.txt
Hide Output of File in Linux
Disguise Output of File in Linux

6. Write Output to Privileged File

Typically, we have to write the output to the privileged file. Nonetheless, we can’t obtain this utilizing simply the tee command. In such eventualities, we are able to use the mix of the sudo and tee instructions.

First, let’s change the possession of the file utilizing the chown command:

$ sudo chown root:root output.txt

Subsequent, let’s append some textual content to the output.txt file utilizing the next command:

$ echo "tecmint.com" | sudo tee -a output.txt

Lastly, let’s confirm that the file has been up to date efficiently.

Write Output to Privileged File
Write Output to Privileged File

7. Edit Privileged File in Linux

In an analogous means, we are able to use the mix of the sudo and tee instructions to edit the privileged file. To know this, first, let’s open a privileged file in a Vim editor and add some textual content to it:

$ vim output.txt

Now, let’s use the beneath Vim command to replace the file:

:w !sudo tee %

8. Ignore Interrupts Sign (SIGINT)

In Linux, we use the Ctrl+c key mixture to ship the SIGINT sign. The default conduct of this sign is to terminate the method. Nonetheless, we are able to use the -i choice to ignore the interrupt:

$ echo "tecmint.com" | tee -i instance.txt
$ cat instance.txt
Ignore Interrupt Signal
Ignore Interrupt Sign

On this information, we realized in regards to the tee command utilizing some sensible examples. Customers can consult with this information whereas working with Linux programs from the command line interface.

Are you aware of some other greatest instance of the tee command in Linux? Tell us your views within the feedback beneath.

If You Respect What We Do Right here On TecMint, You Ought to Take into account:

TecMint is the quickest rising and most trusted group website for any sort of Linux Articles, Guides and Books on the net. Hundreds of thousands of individuals go to TecMint! to look or browse the hundreds of printed articles out there FREELY to all.

In case you like what you might be studying, please take into account shopping for us a espresso ( or 2 ) as a token of appreciation.

Support Us

We’re grateful to your by no means ending assist.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments