Sending output to more than one place

If you need the output of a command in more than one place, and the command does not need to run for long, the most straightforward way is to save it to a single file first, and then use cat or cp to send it wherever it needs to be copied. This is simple and easy to read and understand, and is the recommended way to do it if the command does not need to run for long.

However, for situations where you want the output to go to more than one place while the command is actually running, you can use the tee command, which copies all of its input to any files named in its arguments:

$ printf 'Copy this output\n' | tee myfile
Copy this output
$ cat myfile
Copy this output

Notice that the output went to both the terminal and to myfile. This works when multiple files are specified, too:

$ printf 'Copy this output\n' | tee myfile1 myfile2 myfile3
Copy this output
$ ls myfile*
myfile1  myfile2  myfile3

Each of the three files will end up with the same content.

You can use theĀ -a option of tee to append to files rather than overwriting them, much like using >> instead of >.