Mar 16, 2022
Linux Tee Command with Examples
The Linux tee command reads standard input and writes it to both standard output and one or more files. In normal output redirection, the lines of the command will be written to a file, but we cannot see the output at the same time. Using the tee command we can achieve this! That’s why in this tutorial, we’ll show you all the Linux tee command basics to get you started!
Linux Tee Command Syntax and Uses
Be you start using the Linux Tee command, you should access your VPS using SSH. If you’re having trouble, check out our PuTTY tutorial.
The basic syntax for the command is:
wc -l file1.txt | tee file2.txt
The above command will check the line count of file1.txt and output the result in the terminal and save it in file2.txt.
Sending the Linux Tee Command Output to Other Commands
While using the Linux tee command we get an output in the terminal, which we can pipe to another command for processing. The following command will list the files inside the folder and using the first pipe will write the output to the file test.txt and pass the output to the third command – grep to identify the files with the string py in them:
ls | tee test.txt | grep 'py'
Other Linux Tee Command Operations
If you want to learn more cool Linux tee command features, you should know how to open its manual! This will make the use of correct syntax much easier!
Most users tend to copy and paste the commands into the terminal, but we urge you to take the time to type them, so you can really get the hang of them, and understand the Linux tee, and other command syntax better.
To bring up the documentation you can use:
Every command comes with version information. It can be checked using:
By default, the tee command will overwrite the file with the output of the initial command. Which can be overridden by using an append option using -a switch.
ls | tee –a file.txt
With the Linux tee command, we can also save the output of a command to multiple files. The use of this option is for processing the output of the command multiple times in a shell script:
ls | tee file1.txt file2.txt file3.txt
Like with standard commands appending with >, the errors and stdout are handled differently in tee as well. Regular | pipes will save only the standard output to the file, and if we need both standard output and the error output, we need to use |& with the Linux tee command.
Normal standard output copy:
|command > file.txt||The regular way of redirecting the standard output|
|command | tee file.txt||Using tee with overwriting|
|command | tee -a file.txt||Using tee with appending|
Both standard output and error streams:
|command &> file.txt||The regular way of redirecting the standard output and error|
|command |& tee file.txt||Using tee with overwriting|
|command |& tee -a file.txt||Using tee with appending|
Writing files using elevated privileges in the vim editor is another advantage of the tee command. In a highly secured environment, normal Linux operations are carried out using nonprivileged users. To perform administrative tasks with admin privileges we use the sudo command.
Sometimes we need to edit the files which need admin privileges.
Abandoning changes and reopening a file using necessary privileges using sudo is one of the options. If we are using the tee command, we can avoid this situation by writing the file in the initial stage itself without abandoning the changes by using the elevated privilege option. When writing in the vim editor, the syntax would be as follows:
:w !sudo tee %
The Linux tee command is a handy utility for installing scripts and is unfortunately rarely known by the system admins. Once you understand the usage of this command, you will surely use it instead of output redirection using >. Good luck with improving your project! See you in the next tutorial.