An intro to redirection in Linux

Powerful pipelines!

An intro to redirection in Linux

I/O redirection allows us to redirect input and output of commands to and from files, as well as connect multiple commands together into pipelines.

Standard Input, Output, and Error

Output consists of two types

  • The program's results. Known as Standard Output or stdout
  • Status and error messages. Known as Standard Error or stderr

In addition, some programs take input from standard input stdin which by default is attached to the keyboard.

By default, both stdout and stderr are linked to the screen and not saved into a file. Redirection allows us to change where output goes and where input comes from.

Redirecting Standard Output

To redirect stdout to another file instead of the screen, we use the > redirection operator followed by the name of the file.

For example, we could tell the shell to send the output of the ls command to the file ls-output.txt instead of the screen

ls -l /usr/bin > ls-output.txt

We can see that the ls output was not sent to the screen, but to the ls-output.txt file.

Keep in mind that using the redirection operator will overwrite the destination file. To append, we use the >> redirection operator.

Redirecting Standard Error

To redirect stderr we must refer to its file descriptor. The shell references stdout stdin and stderr internal as file descriptors 0, 1, and 2, respectively. We can redirect stderr with this notation:

ls -l /bin/usr 2> ls-error.txt

Redirecting Standard Output and Standard Error to One File

There are two ways to accomplish these, first let's use the traditional method, which works with old versions of the shell:

ls -l /bin/usr > ls-output.txt 2>&1

First we are redirecting stdout to the file ls-output.txt and then we redirect file descriptor 2 stderr to file descriptor 1 stdout using the 2>&1 notation.

Keep in mind that the order of the redirections matter, the redirection of stderr must always occur after redirecting stdout.

Recent versions of bash provide a second, more streamlined method for performing this combined redirection

ls -l /bin/usr &> ls-output.txt

You can still append using the >>

Disposing of Unwanted Output

The system provides a way to redirect output to a special file called /dev/null which is often referred to as a bit bucket. It accepts input and does nothing with it.

ls -l /bin/usr 2> /dev/null

Redirecting Standard Input

Using the < redirection operator, we can change the source of stdin from the keyboard to a file.

cat < sample.txt


Using the pipe operator |, the stout of one command can be piped into the stdin of another. less is an example of this

ls -l /usr/bin | less

it is possible to put several commands together into a pipeline. The commands used this way are referred to as filters. Filters take input, change it somehow and then output it.

The tee command

The tee command reads stdin and copies it to both stdout and to one or more files.

ls /usr/bin | tee ls.txt | grep zip

Subscribe to madebygps

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.