Linux tee command - redirect output to two directions at once

Today I ran into a need for my old friend the Linux tee command. With the tee command you can read input from an input stream, and split the output stream in two directions, so it is both displayed on screen (stdout) and also re-direct it to a file. I needed to do this today when I wanted to monitor something that was running slow, and also keep an output log of the long-running process.

Simple Linux tee command example

Here's a simple example of how to do this. Run the following command to get a directory listing on your terminal, while also redirecting the output to a file named poop.out:

ls -al | tee poop.out

When you run this tee command you should see the output you would normally expect to see appear on your terminal. But, the difference here is that if you now cat out the file named poop.out like this:

cat poop.out

you'll see that a copy of the contents were also sent to that file.

As you can see, the Linux tee command is exactly what you need for situations like this, where you'd like to see the output on screen, but also capture it into an output file.

Note: The script command is loosely related to this topic. I'll write a quick tip about it in a future blog entry.