How to Use tee command in Linux?

The tee Linux command that reads from the standard input and writes to both standard output and files. It is used mostly with other commands through the concept of pipes. This command performs two tasks simultaneously i.e. writes output to one or more files and also displays on the standard output.

In this article, you will know how to use the tee command in Linux along with some examples.

Syntax of tee command in Linux

The syntax of using the tee command in Linux is given below –

tee [option]...[file]..

Where you can find the detailed list of options on the tee command man page and the file is one or more files where data is to be written.

Usage of the tee command in Linux

The tee command is generally used with other commands. Where it takes the output of the given command as its input and displays the output on the terminal and also writes it to one or more files.

For example –

ls -l | tee file_permission.txt

You can see the output of this command in the given image –

print and write the output of tee command in one or more files

The content displayed here is also saved in file_permission.txt. You can see it by using the given command –

cat file_permission.txt

Here you will see the same output –

read the file written by tee command

Display the output of tee command and write it to multiple files

If you want to write output into multiple files you can do this you will need to mention multiple filenames in the above command. Now see the example below –

ls -l | tee file_permission1.txt file_permission2.txt

This will write the output in both the given files.

How to append the output of tee command to a file

By default, the tee command overwrites the content of the given file. To append the output into the given file you will need to use the option -a with the tee command. See the given example –

ls -l | tee -a file_permission.txt

Here the output of the command ls -l will get appended to the file file_permission.txt.

How to use sudo with tee command to write to a file

You can write to a file that requires the root privilege by using the sudo with tee command.

For example –

ls -l | sudo tee file_permission.txt

Where file_permission.txt is a file that requires root privilege.

You can explore other options by seeing them on the manual page of the command. Use the following command to see the man page of the tee command.

man tee

Conclusion

Similarly, you can use this command with some other commands if you want to save and display the output. Now if you have a query then write us in the comments below.

Leave a Comment