HomeLinuxLinux CommandsCommand Options and Examples of Tee Command in Linux

Command Options and Examples of Tee Command in Linux

In Linux, terminal commands are the handiest and most useful things that one might be focusing on learning. If you’re a new or professional Linux user, you might have been using many commands to add or sum the results of more than one command in the shell. But, Linux offers you to join the output of two different commands and get the final output in one single output. The tee command in Linux is one of the most used and handy tools that you should learn for making your Linux journey quick and fast.

For instance, if you want to download and extract a file on your Linux system, you can efficiently do that with a tee command by joining the wget command and the tar command. Knowing the pipe or tee command would be both fun and satisfactory to learn.

Tee Command in Linux


The tee Command in Linux is which reads standard input and writes it to both standard output and one or more files, effectively duplicating its input. The tee command is normally used to split the output of a program so that it can be both displayed and saved in a file. The concept of the Tee (T) came from the form of a Tee shape in plumbing.

This command allows you to send the output to a file in the standard output format. If you’re familiar with the Pipe command of Linux, you must know that it works in a linear format, while the Tee command can redirect the output to a different path and save the output.

Now, you could have saved the result into the file with two different commands, but it would be a bit of a hassle. At the same time, the Tee command allows you to join all these tasks with just a single command. In this post, we will see how to use the Tee command in Linux.

1. The Basic Syntax for the Tee Command 


To get started with a command on Linux, we must know the basic rules, syntaxes, and the method of how to put them in between inside a command. Here it comes; in the below command pattern, you can see that the tee command goes in the middle of two commands or the middle of a command and an option. Actually, it can also work as a conjunction in Linux command.

[command] | tee [options] [filename]

2. Ping with Tee in Linux


Checking the internet connectivity via the ping command is a very popular command for both Linux and Windows users. However, as we have discussed earlier that the tee command can print and save the data in a different file simultaneously.

- -

The below command will give you the ping result on the terminal screen, and the output will be saved in the exact same format you see on the screen for future reference. Later, you can use the cat command to see the output.

$ ping google.com | tee output.txt
$ cat output.txt

tee ping command on linux

3. Use the Tee Command To Append Text


In Linux, the term append refers to the method of adding or showing the result with a value given by the user. Let me make it easy for you; let’s assume I’ve two text files that are named file1.txt and file2.txt. And here are the texts that I put inside the files.

  • File1.txt:  ubuntu is fun
  • File2.txt: ubuntuPIT is fun

Now, if I apply the Tee command for these two files, I can replace the final output of the text with the second one. This is actually not replacing the value, whereas we are appending the value ‘Ubuntu’ with the value ‘UbuntuPIT’

Cat file1.txt
File1.txt: ubuntu is fun
File2.txt: ubuntuPIT is fun

We can now execute the given tee command on our Linux terminal shell.

$ wc -l file1.txt|tee -a file2.txt
$ cat file2.txt

cat command with tee in linux

4. Tee in Conjunction with sudo


As I have already said earlier that in Linux, the Tee command can be used to link or join the output of two commands; here, we will see how to use the tee command as a conjunction. Please note if the file we want to join is under the root user, and if you’re not the root user, you may need to add the sudo before the tee command, as shown below.

For instance, the below command won’t work and may fail due to limited permission.

sudo echo "newline" > /etc/file.conf

You can just put the sudo just before the tee command to make the command executable.

echo "newline" | sudo tee -a /etc/file.conf

5. Tee Help


The following command is pretty much self-explanatory. The help guide that tee command supplies are helpful for getting started with it. It provides syntaxes, a user guide, and the guide on how you can face error outputs.

tee --help

tee --help

If you’re having an issue with the Tee command on your Linux machine, make sure that you’re using the latest version of Tee on your system. The below-mentioned command will let you check the Tee version.

tee --version

6. Sum Up Char In Tee


If you’re willing to know the total number of characters in your file, you can simply use the tee command, and interestingly you can save how many characters are in the file into a different file.

wc -l file1.txt| tee file2.txt

7. Use Tee with DH


Normally, the dh command can show the disk usage and monitor the system. If you need the live record of your system statistics, just use the dh command with the tee command in Linux. The text file will be saved inside the home directory of your Linux filesystem.

df -h | tee disk_usage.txt

dh with tee in linux

Later, you can use the cat command to see the output.

$ cat disk_usage.txt

8. Write to Multiple File


Previously, we have seen how to combine more than one command through a tee command in Linux. Apart from that, in Linux, you can keep your final output of any command into multiple files at a time. This might help you if you’re working on some sort of project or task where you need to deliver the same output to multiple users.

The below-mentioned command shows how to put the file names one after another right after the command.

$ command | tee file1.out file2.out file3.out

Insights


The cross-platform Tee command can be very handy if you’re doing something that requires saving the output while still working on the command. In the entire post, I’ve described the notion and most used examples of the Tee command on Linux. The above-mentioned commands would be executable in all major Linux distributions. You can add or modify or tweak the commands with the Tee command to get your desired result.

If you find this post useful and informative, please share this with your friends and the Linux community. You can also write down your opinions about this post in the comment section.

Mehedi Hasan
Mehedi Hasan
Mehedi Hasan is an online blogger who mostly writes on Chromebook, ChromeOS, Ubuntu, Linux, and Everything about Trending Technology.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Post

Must Read