Skip to content

How to redirect standard output in Linux?

How to redirect standard output Cover?

Command line has a lot useful features, but one of the most important is the ability to redirect the input and output of commands to and from files, and to connect multiple commands together to form powerful command pipelines.

Standard input, output and error

The terminal takes input from a facility known as standard input (or stdin, which is by default the keyboard), and produces an output consisting of two types. The first type is the result of the command, and the second are the status and error messages which give us descriptive information about the command itself. The results are being sent to the standard output file (or stdout) and status messages are being sent to another file called standard error (stderr). By default, both are outputted to the screen.

Why is this important? Because we could use I/O redirection to change where the input comes from, and where the output goes.

Redirecting Standard Output

By default, the output is displayed on the screen, and the input comes from the keyboard, but we can redefine both the input and output. To redirect standard output to another system beside the screen we can use the ‘>’ operator followed by the name of the file.

ls -l /usr > ls-text-file.txt

What we have actually done above is that we created a listing of the /usr directory and sent the results to the ls-text-file.txt file (1). Why would we do something like this? Because we might need the output of a command stored in a file (a log of a failed command comes to mind). Now, we can simply run cat ls-text-file.txt (2) and we would see the contents of that file.

Redirecting Standard Output
Redirecting Standard Output

What if the directory doesn’t exist?

What if we would write something like this where the directory doesn’t exist (/usr/usr2 is a made-up dir):

ls -l /usr/usr2 > ls-text-file.txt

Well, we would get a nasty little message:

ls: cannot access '/usr/usr2': No such file or directory

But wait a second. Why did the message appear in the terminal, rather than in the file? Well, because (more about standard error in the next section) standard error messages are not sent to the standard output. Instead, they are sent to the standard error, thus the message appeared in the terminal rather than in the file.

This asks a question – what happens to the file we tried to write our results to?

If we take a look at the screenshot below we actually see… that it is empty! When we try to redirect the standard output to a file – the file is ALWAYS being rewritten. Even though the command generated an error the file is empty (and it would be overwritten if it had any content before)!

A good method to prevent files overwriting is to use the ‘>>’ operator which actually appends the output to the content already in the file. If the file does not exist it is created, otherwise, the content is just concatenated.

Redirecting Standard Output
Standard Output

Just to recap. We tried to list a directory that doesn’t exist (1). Then we listed the contents of the file (2) and the size column returned 0. Just to make sure we ran the cat command (3), proving that the file is completely empty. Then, using the append operator we redirected an output that exists to the same file (4). We listed the file again just to prove that it now has content (the size column being 661). Lastly, we appended the same command again (6), and we listed the ls-text-file.txt one more time (7). We notice that the size increased, by the same value, meaning that we appended the same content two times.

Conclusion

We only mentioned one part of the whole I/O redirect story! The next part which will be about Redirecting Standard errors will come soon enough.

You can read more Linux related posts on the Linux subsection here or click on any of the links below:

2 thoughts on “How to redirect standard output in Linux?”

  1. Pingback: How to redirect standard error in Linux? | The Dukh Chronicles

  2. Pingback: Cat command and pipelines in Linux | The Dukh Chronicles

Comments are closed.