Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Linux Tee Command Syntax and Uses
#1


Linux Tee Command Syntax and Uses



Before you start using the Linux Tee command, you should access your VPS using SSH.


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:


tee --help

Every command comes with version information. It can be checked using:


tee --version

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 the -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  
command | tee file.txt 
command | tee -a file.txt 

Both standard output and error streams:


command &> file.txt  
command |& tee file.txt 
command |& tee -a file.txt 

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 %



Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)