Tee Command in Linux Explained with Examples
If you want to display the output of a command and save it to a file simultaneously, tee command is what you need. Learn various ways of using tee command in Linux.
The tee command reads from the standard input and writes to both standard output and files.
The result is that you get to see your command’s output as well as save it to a file at the same time.
In other words, you have one input that is channeled to two outputs.
This all makes more sense if you are familiar with the redirection concept in Linux.
The command is named after the tee connector used in electricity, plumbing, and other industries. They are called tee because they resemble the letter ‘T’.
Now that you understand the purpose of the tee command, let’s see how to use the tee command in Linux.
Tee command examples
The tee command has a simple syntax:
tee [OPTION] [FILE]
Remember that tee reads from the standard input so almost all the times, you’ll use it in the conjugation of another command.
Let me show you some examples.
1. Display command output and save it to a file
Let’s take a basic case where you want to count the number of lines in a file. You want to see how many lines are there in the file and save that number to another file.
abhishek@itsfoss:~$ wc -l agatha.txt | tee count.txt 20 agatha.txt
The file count doesn’t exist so it will create a new file named count. If you see the content of file count, you’ll see the same output that you saw on the display.
abhishek@itsfoss:~$ cat count.txt 20 agatha.txt
Note: By default, tee command will overwrite the content of the file. If you want you can use the append option -a with it:
wc -l agatha.txt | tee -a count.txt
2. Display command output and save it to multiple files
If you want to save the command output to multiple files, you can do that as well with tee command. You just have to specify the files.
abhishek@itsfoss:$ wc -l agatha.txt | tee count1.txt count2.txt 20 agatha.txt
You can verify that the same output has been stored in the two mentioned files.
abhishek@itsfoss:~$ cat count1.txt count2.txt 20 agatha.txt 20 agatha.txt
I hope you knew that you can see multiple files together with the cat command in Linux.
3. Parse the command output to another command while saving it to a file
You don’t always need to see the command output. Since it is standard output, you can pipe it to another command.
Take the command below for example.
abhishek@itsfoss:~$ ls -l | tee count.txt | wc -l 7
What happens here is that the output of ‘ls -l’ command is piped to the tee command. Now the tee command stores the output of ‘ls -l’ in the count.txt file but instead of displaying this output, it is piped to ‘wc -l’ command that counts the number of line.
The output you see in this case is the output of the ‘wc -l’ command. The output of ‘ls -l’ command is stored in the file count.txt.
abhishek@itsfoss:~$ cat count.txt total 20 -r--r--r-- 1 abhishek abhishek 456 Dec 11 21:29 agatha.txt -rw-r--r-- 1 abhishek abhishek 0 Jan 10 16:03 count.txt -rw-r--r-- 1 abhishek abhishek 356 Dec 17 11:39 file1.txt -rw-r--r-- 1 abhishek abhishek 356 Dec 17 09:59 file2.txt -rw-r--r-- 1 abhishek abhishek 356 Dec 11 21:35 sherlock.txt drwxr-xr-x 3 abhishek abhishek 4096 Jan 4 20:10 target
Using the tee command is really up to your imagination and your situation. I find it handy when I have to analyze a lengthy output like a log file. Seeing it in real time and storing in a file for future reference helps a bit and saves me some time.
I hope you liked the tee command examples I listed here. If you think of some really awesome way to use this command, please share it with the rest of us in the comment section.