Redirecting and displaying standard output and error

Most Linux sysadmins will know this little trick for redirecting standard output and standard error (i.e. ‘all’ output from a command) to a file:

make &> /tmp/build.log

However, sometimes you’ll want to monitor the output on the screen as well as redirecting to a file. For example, when I’m building a big program I want to keep my eye on where things are up to, but I also want a copy sent to a file so I can examine it if there are any errors. I could open a second terminal and run tail on the file, but there’s a more elegant solution:

make 2>&1 | tee /tmp/build.log

The tee command reads from standard input and writes to standard outputĀ and a file. So in the above pipeline we’re saying:

  1. Redirect standard error to standard output (2>&1).
  2. Pipe standard outputĀ to the standard input of tee.
  3. Display standard input on standard output and write it to a file.

Tee is part of the GNU coreutils package, so it should be installed by default on pretty much every Linux system.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.