Linux Command Line Redirection To Files

There are many times when you’re working on the command line where you need to redirect the output of a command into a file.

New users typically use the following:

mycommand > myoutputfile.txt

The command is not wrong – it’s perfectly valid as long as you understand what might be missing.

The issue with it is that the text file might not contain any error messages. And that is because of the way Linux (and Unix) treat standard output vs error output.

To understand how you might be missing data in the output file, we need to start with some basic Linux background.

Background

Linux generally treats almost everything as a file or virtual device. Because of this, it means that any output (and input) can be redirected to/from alternate locations.

Linux also sends regular output and error output to two different virtual devices. When you’re working on the command line those different devices both redirect to your screen by default.

In documentation and other articles you’ll see these two output locations referred to as ‘stdout’ and ‘stderr’ or some similar variation.

The combination of virtual devices and the existence of two different output locations for regular output and error output means that when you try to redirect output to a file, you have to redirect BOTH locations. Otherwise you will not capture all output.

In this article we’ll cover the following:

  • Send just regular output to a file
  • Send just error messages to a file
  • Send both regular output and error messages to a file

Send just regular output to a file

To send regular output to a file just use the redirection operator “>” as follows:

mycommand > myoutputfile.txt

If you want to append to an existing file, use “>>”

mycommand >> myoutputfile.txt

This is the command / operators that most new users are familiar with.

Send just error messages to a file

To capture just error messages into a file:

mycommand 2>myerrfile.txt

The “2” represents the Standard Error virtual device.

The most important thing to notice about the above command is that there are NO SPACES between the ‘2’, ‘>’ and ‘myerrfile.txt’.

Send both regular output and error messages to a file

To capture all output from a command into a file, you use the following:

mycommand >> outputfile.txt 2>&1

As with the prior section, the most important thing to notice about the above command is that there are NO SPACES between the ‘2’, ‘>’ and ‘1’.

If the command looks weird, it’s just because of how Linux handles things – but lets take a closer look and see why it’s constructed this way.

Here are some components of the command and what they represent:

  • “1” means Standard Output
  • “2” means Standard Error
  • >> outputfile.txt means redirect all Standard Output to the file output.txt.

The 2>&1 simply tells Linux to send all errors to where ever Standard Output is being sent. Since Standard Output is being redirected to a file it means that errors are sent to the same file.

So, reading from left to right, the command states:

Send the Standard Output of mycommand to outputfile.txt and then send Standard Error messages to wherever Standard Output is being sent [aka outputfile.txt].

Bonus: Send Regular Output and Error Output To Two Separate Files

While most use-cases would have both regular and error output going to the same file, there are many instances where it’s better to have them go to different files. Here is how you do that:

mycommand >> outputfile.txt 2>myerrfile.txt

Reading from left to right and keeping in mind that ‘2’ means Standard Error, the command states:

Send the Standard Output of mycommand to outputfile.txt and send Standard Error messages to myerrfile.txt.

Additional Notes

It is up to each individual program to output error messages to the correct device. The developers of some programs might choose to output both regular messages and error messages to Standard Output. Most built-in Linux and commonly used commands and packages send errors to the appropriate output device which means command output redirection into files should work as you expect.

WP CLI is one of the culprits where some components (especially third party components) are inconsistent with where they send output. So, when using this and redirecting to files, it’s best to capture both Standard Output and Standard Error so you don’t miss something (unless you don’t care about WP CLI errors being captured in your files.)

Was This Article Useful? Or do you have questions or comments about it (or our products & services)? We'd love to hear from you!

Please enter your name.
Please enter a message.
You must accept the Terms and Conditions.
Please check the captcha to verify you are not a robot.

Automatic Notification Of New Articles

Sign up to get automatic notifications of new articles.  This is a different list than our standard list - you only get new articles once a week (usually on Mondays).  No other emails will be sent unless you sign up for our general list as well.

Posted in