Linux tee command with options description

Unix or Linux tee command is similar to ‘cat’ command with only one difference. It reads the input and writes in in a file also outputs in the terminal.

linux tee command man page - partial
linux tee command man page

Sample syntax with tee command :

$ cat <filename>  | tee<new file>

Like example of tee command :

$ cat weeks.txt | tee new_file.txt

See your terminal after run the command, file new_file.txt is create with the help of tee command. And the contents of the new_file.txt is same as the weeks.txt. Also the contents of weeks.txt is shown in terminal.

Lets see a live example

linux tee command simplest example

In this example, we transferred the output of first command to a file. Firstly, we’re in a directory /var/log. This is the directory of ubuntu file systems to store logs. There is a file named dmesg, which stores the system logs. wc -l dmesg, outputs the line count of the file. When we pipe the command to tee /tmp/test.txt, the output of the wc -l dmesg command gets written to the file /tmp/test.txt, and also gets printed in the terminal.

Use tee -a if you want to append to the file

If you want the output to append to the file instead of replacing the contents of the file use tee -a.

Leave a Reply