LINFO

Redirection Definition



Redirection is the switching of a standard stream of data so that it comes from a source other than its default source or so that it goes to some destination other than its default destination.

In Unix-like operating systems each command, and therefore each process, is automatically initialized with (i.e., assigned) three data streams: one input stream, called standard input, and two output streams, called standard output and standard error. All consist of plain text (i.e., human-readable alphanumeric characters) and are considered to be a special type of text file, and all are associated with the console (i.e., the keyboard and the text-mode display screen) by default. A command specifies a program to be run. A process is an instance of a program in execution (i.e., running).

Standard input is the source of input for a command; it is by default text typed in at the keyboard, but it can be redirected to come from some other source, such as a file or another program. Standard output and standard error are the destination of the output and the error messages from commands, respectively; they are the display monitor by default, but they can be redirected to go to some other destination, such as a file, a printer or another program.

The use of standard streams has the advantages that processes do not need to be aware that redirection is occurring and that it simplifies program development. It makes redirection a simple matter, and thus it is a major factor in the flexibility and convenience of Unix-like operating systems.

Arguments

Redirection devices can be broadly classified into two categories: arguments and redirection operators. An argument is the name of a file or other data that is provided to a command in order for the command to use it as an input, and it redirects standard input from being the keyboard to being that file or data.

If the head command, which by default reads the first ten lines of text, is used without any arguments, i.e.,

head

it will read the first ten lines of text typed in at the keyboard, i.e., its standard input, and repeat those lines to standard output, which by default is the display monitor.

This can be seen by pressing the ENTER key, which results in moving to a new line for typing any desired text. After entering some text, pressing the ENTER key again to move to another new line allows the command to be executed by simultaneously pressing the CONTROL and d keys. This causes the text that was typed at the keyboard to be read from standard input and then be written to the next line(s) of the display screen.

When a file named, for example, file1 is used as an argument with head, standard input is redirected from being the keyboard to being that file, i.e.,

head file1

Standard input could just as easily be redirected with multiple arguments so that the input data comes from multiple files, for example

head file1 file2 file3

In this case, head will by default read the first ten lines from each file and write them to the display screen.

Redirection Operators

Redirection is usually thought of in terms of redirection operators, which can be used to switch both the source and the destination of standard streams of data in commands. They can be classified as input, output, error and pipe.

Redirection operators are formed mainly from three characters: the leftward pointing angle bracket ( < ), the rightward pointing angle bracket ( > ) and the vertical bar ( | ). In some cases the ampersand ( & ) and the numeral 2 are also used.

To redirect standard input so that a command obtains its input from a file instead of the keyboard, the input redirection operator, which is represented by a leftward pointing angular bracket, can be used. For example, the standard input for head can be redirected to be a file named file4 with the following:

head < file4

This has the same result as using file4 as an argument, i.e.,

head file4

However, the mechanism is different.

Because most of the commonly used commands can accept arguments, the input redirection operator is not as frequently employed as some of the other redirection operators. However, it can be helpful in some situations, and it is certainly useful to be aware of it.

Standard output and standard error both are by default the display screen. Thus, when a command is issued in the absence of any redirection (e.g., to a file or printer), these two output streams will be displayed on the monitor.

The output redirection operator, a rightward pointing angular bracket, can be used to redirect standard output. For example, the output of the cat command, which is commonly used to read files, could be redirected when reading a file named file5 from the display screen to a file named file6 with the following:

cat file5 > file6

Thus, the contents of file5 are written to file6 and are not visible on the display monitor (because they are not sent there). There is no effect on the contents of file5. This can be easily confirmed by using cat or any text editor (such as vi) to read these files.

One way in which standard error can be redirected is by using the standard error redirection operator, which consists of a rightward facing angular bracket preceded directly (i.e., without any intervening space) by the numeral 2, i.e., 2>.

(The technical reason for the numeral two in the standard error redirection operator is that each of the three data channels is controlled via a file descriptor, which is a numerical identifier maintained by the operating system: 0 for standard input, 1 for standard output and 2 for standard error. Although the appropriate file descriptors can also be used with the standard input and output operators, it is not necessary.)

One of the many acts that will produce an error message is the use of the name of a non-existent file as an argument for a command. An example is supplying the name nofile as an argument to head even though a file with that name does not exist, i.e.,

head nofile

In such case, an error message similar to the following will appear on a new line on the monitor screen: head: nofile: No such file or directory .

The standard error from this example can be easily redirected from the monitor to a file. For example, the following would send it to a file named file7:

head nofile 2> file7

If the file to which standard output or standard error is redirected does not yet exist, it will be created. If a file with the same name already exists, its contents will be overwritten.

An alternative is to use the append operators, which add the output to the end of designated file rather than overwriting it. The standard output append operator is designated by two successive rightward pointing angle brackets, and the standard error append operator is designated by the numeral 2 followed by two rightward pointing angle brackets. This is useful for log files. For example, the following will append the error message to the end of what is already in the file named logfile:

head nofile 2>> logfile

Pipes differ from the other redirection operators in that they redirect the standard output of one command to become the standard input of another command. In the following example, the standard output of the ls command, which by default provides a list of the files and directories in the current directory (i.e., the directory in which the user is currently working), becomes the input for wc, whose default behavior is to count the number of lines, words and characters in whatever is provided to it:

ls | wc

This ability to redirect output to other commands allows any number of commands (inclusive of their options and arguments) to be strung together to form pipelines of commands that can perform highly specific tasks that would be very difficult or tedious by any other means.

Combining Redirection Types

Various types of redirection are frequently used together. As a trivial example, the following uses an input redirection operator and an output redirection operator to read the first ten lines of file8 and write them to file9:

head < file8 > file9

As an example of the separate redirection of the two output streams from a single command, the wc command can be used to attempt to count the lines, words and characters in two files: nonfile, which does not exist, and file10, which does exist. Standard output is redirected to the file wordcount and standard error is redirected to the file errorlog:

wc nonfile file10 > wordcount 2>> errorlog

Likewise, pipes can be used together with other redirection operators. For example, the output of ls | wc from the earlier example could be redirected to a file named file11 a follows:

ls | wc > file11






Created June 15, 2004. Updated May 24, 2005.
Copyright © 2004 - 2005 The Linux Information Project. All Rights Reserved.