Redirecting the Linux Standard Input and Output

Controlling Data Streams in Linux

The Linux Standard Input, Output and Error - Mark Alexander Bain
The Linux Standard Input, Output and Error - Mark Alexander Bain
Not all Linux inputs have to be from the keyboard and not all Linux outputs have to be to the screen - this article looks at how to change these data streams.

Linux has two data streams that the user interacts with - even if the user doesn't realize that they are data streams; these are:

  • the standard output (or stdout) - known to the user as the screen
  • the standard input (or stdin) - known to the user as the keyboard

However, it is very easy to change the data streams so that:

  • the standard output is written to a file
  • the standard input is read from another file

And there is actually a third data stream - the standard error - although the user only normally sees this as part of the standard output (since all errors are displayed on the screen).

Using the Standard Input and Output

Before looking a the redirection, it's worth looking at the standard output in normal usage:

$ ls -1
How to Redirect Standard Input and Output.html
process_article
writing_status
writing_status~

Here a command has been typed in via the keyboard and the result shown on the screen - the keyboard supplies the input for the standard input and the screen is the output for the standard output.

Redirecting the Standard Output

The standard output is redirected by using the > sign:

$ ls -1 > files.txt

There will be no output to the screen, instead a new file will be created (in this case files.txt) and if that file is examined then it will be found to contain a list of the directory contents.

If the command is repeated then it will result in an error if noclobber has been set (to prevent files from being overwritten accidentally) for instance:

$ set -o noclobber
$ ls -1 > files.txt
-bash: files.txt: cannot overwrite existing file

If that's the case then the overwrite can be forced by adding an exclamation mark:

$ ls -1 >! files.txt

Another option is to append the standard output to the file by using >>:

$ ls -1 >> files.txt

Redirecting the Standard Input

Unsurprisingly the standard input is redirected by using the < sign:

$ ls -1 > files.txt
$ cat < files.txt
How to Redirect Standard Input and Output.html
process_article
writing_status
writing_status~

In many cases the < is not needed - if the command is given a file name then it will use that as then standard input, for example both of the following will use files.txt as the standard input:

cat < files.txt
cat files.txt

Working with the Standard Error

The third data stream that Linux uses - the standard error - appears to be the same as the standard output, but it will bypass any the standard output redirection; so, for example, the following will result on an error message on the screen but not the target file:

$ wc -l nofile.txt > count.txt

In this case the standard error must be redirected to the standard output and then to the file:

$ wc -l nofile.txt 2>&1 1>& count.txt

The target file (count.txt) will now contain the error message.

Summary

For the normal Linux user:

  • standard output is the pc screen
  • the standard input is the keyboard

However, the standard output can be redirected to a file:

  • > - redirects the standard output to a file, overwriting the file if it exists
  • >! - forces a file to be overwritten when noclobber has been set
  • >> - appends the standard output to a file

And, of course, the standard input can be redirected by using the < sign.

Finally the standard error needs to be handled - and will have to be passed to the standard output and then to a text file:

  • 2>&1 1>&

By combining all of these the user has complete control of each of the Linux standard data streams.

Mark Alexander Bain - Mark Alexander Bain is a writer, Mo Bro and consultant for all aspects of software development at dsquared. He has also written regularly ...

rss
Advertisement
Advertisement
Advertisement