7.1 Advanced Directory and File Management Using the Command Line
7.1.5 Input / Output redirection
All central processing unit (CPU) operations have input and/or output (I/O). The keyboard, for example, provides standard input (stndin) while the monitor displays standard output (stndout) and standard error (stnderr).

Every UNIX command has a source for standard input and a destination for standard output. Input and output for our purposes here are defined with respect to a UNIX command. The input to a command is normally from the keyboard, although it can come from a file. The output from a command normally goes to the monitor or screen. Errors that might result from a command are another form of output and are also displayed on the screen. Figure shows input to a command and the two types of output from it. Input is sending data to a command. Output is receiving data from a command. Errors are generated by the results of an incorrect command line.

The UNIX computing environment enables command I/O to be controlled using redirection. This is useful when attempting to save the output of a command to a file for later viewing, sending the contents of a file as an email message, or redirecting error messages to a file for use when debugging a program. There are several metacharacters used as redirection symbols. Output redirection uses the right angle bracket (>) which is also referred to as the greater-than symbol. Input redirection uses the left angle bracket (<) or less-than symbol. Error output redirection uses the right angle bracket preceded by the number two (2>). The general format for using redirection is shown below. The File after the redirection symbol can be a text file or a device file. All devices in UNIX are defined by file names. For instance, the screen or monitor device file is /dev/console. It is not necessary to specify this file because it is the default.

$ Command Redirection Symbol File (text file or device file)

Note: Spaces between the command and a redirect symbol are optional.

Redirecting Standard Input
Standard input is not often redirected although it is possible to substitute a text file instead of the normal keyboard input. Note that the left angle bracket or less-than symbol, which allows the command to receive input from whatever file, is present. The default input is the keyboard device. Figure illustrates redirection of input using the
mailx command. In this case the dante file would be sent to the email address using the mailx program. The mailx command invokes the standard command-line mail program and is addressed in more detail later in the course.

command < file

Redirecting Standard Output
Standard output is redirected much more frequently than standard input or standard error. Many commands, such as
ls, cat, head, and tail, generate standard output to the screen, and it is frequently desirable to redirect this output to a file for future viewing or manipulation. By substituting a file name, you can capture the output of a command rather than letting it go to the default monitor. As an example, if you wanted to get a listing of all files in a directory, you could redirect the output from the ls command, capturing it in a file, which could then be edited using a word processor. The output from commands shown in this course was captured in this way.

Creating a New Output File
The right angle bracket or greater-than symbol allows the command to send output to a file. Using the single right angle bracket will create a new file if the file name specified does not exist. If the file name exists, it will be overwritten.

command > file
(creates new file or overwrites an existing one)

Note: In the Korn shell, an option called
noclobber can be set to prevent overwriting of files during redirection. This can be done on the command line by using $ set -o noclobber. To re-enable clobbering, use $ set +o noclobber. To enable/disable clobbering with the C Shell, use the % set noclobber and % unset noclobber.

The first example in Figure illustrates redirection of the output of the ls command. The listing of the contents of the /etc directory would be captured in a file called etc.list which would be placed in the current directory.

Appending to an Existing File
The double right angle bracket can be used if you wish to append (add to the end) to an existing file instead of overwriting it.

command >> file
(creates new or appends to an existing file)

The second example in Figure illustrates the use of the double right angle brackets to create a new file using the cal command and then append to it. The first cal command captures the calendar for October 2000 and the second command adds the calendar for January 2001.

Redirecting Standard Error
Standard error is redirected when it is desired to capture the output of a command which might generate an error. It is useful when developing script files and is normally used by system administrators and programmers. Standard output is usually redirected to a text file, while standard errors are redirected to an error file to assist in troubleshooting if the script or program does not execute properly. To redirect standard error, the right angle bracket with the number 2 in front of it is used. Think of it as the second type of (optional) output from a command. If a command executes with no problems, it will not generate an error.

command 2> file

Figure illustrates the use of the 2 with a right bracket to create an error file. This example captures the error generated from the ls command since the directory does not exist.

Input / Output redirection for Standard Input, Standard Output and Standard Error is summarized in Figure
Interactive Media Activity (Flash, 100 kB)
  Input / Output Redirection

In this media activity, you are logged in as user2 and your current working directory is: /home/user2/dir2. Refer to the Class File Tree Structure by clicking the 'tree' button located on the Menu Bar and type the commands that would accomplish the requested objectives. 
Note: Be sure to press enter after each one. Click on step 1 to begin.