tee command as the man page puts it, reads from standard input and writes to standard output and files. Okay good enough but what does that mean? Well that mean, while using a command, the output is sent to the console. You can redirect it to file by using
>> to a file. That symbol is of output redirection (a summary of that in the end of this post and all the output redirection is covered in separate post).
Let’s see you execute a command, for example
ls and you see the output on the screen. But now you want to send the file listing to a file. What you do is,
ls > file
This way a new file, ‘file’ will be created and the file listing will be redirected to that file. The console will be clear. But what if you wanted the output to be displayed on the screen as well as in the file? You use tee.
ls | tee file
With this command, the output the displayed on screen as well as it goes into the file ‘file’. You can have multiple files with tee.
ls | tee file1 file2 file3
All the 3 files will have same content - the directory listing of current directory.
You can all append to file if you don’t want the contents to be overwritten.
ls | tee --append file
ls | tee -a file
-a append the output to the file and do not overwrite its content.
In bash, you can redirect output to file and overwrite its contents using
> and append the output to file using
>> So for example,
ls > file
ls >> file
The first line will overwrite the contents of file and second line will append without overwriting the contents