PREV UP NEXT The UNIX Computer System at ARE (Edition )

2.5.3: Redirection

Just as you can pipe output from one command into another, its possible to redirect output from a command or command pipeline into a file. This is called output redirection. Suppose for some crazy reason you want a file that contains a list of all files in some directory:

$ ls -al > dir

The `>' character is used to redirect the output of the `ls' command into a file called `dir'. Now this file will contain a detailed listing of all files in the current directory.

If the `dir' had already existed before you executed this command, the shell might complain:

$ ls -al > dir
  -| dir: File exists.

which means you aren't allowed to overwrite or clobber an existing file. You can force the shell to overwrite the file by typing an exclamation point after the `>':

$ ls -al >! dir

In addition to standard input and output, UNIX also defines standard error, which is an output channel where diagnostic messages are sent. Usually both standard output and standard error are connected to your terminal by the shell. Consider the following example:

$ ls -s dog fish
  -| ls: fish: No such file or directory
  -| 12 dog

In this example, the user has asked to see a listing of the sizes of two particular files in the current directory, `dog' and `fish'. The file `fish' does not exist, causing the `ls' command to complain. The file `dog' does exist and is 12Kb in size. The second line of output from this command is sent to standard output by the `ls' command. The first line, complaining about the error, is sent to standard error. The shell attaches both of these output channels to your terminal so you can see them. However, they are distinct channels of information. You can see this by redirecting standard output to a file:

$ ls -s dog fish > duck
  -| ls: fish: No such file or directory
$ cat duck
  -| 12 dog

The diagnostic error message is still sent to the screen even when you redirect standard output to the file `duck'. (The `cat' command simply prints out the contents of its arguments to the standard output). To send both standard error and standard output to a file, the `tcsh' uses the following syntax:

$ ls -s dog fish >& duck
$ cat duck
  -| ls: fish: No such file or directory
  -| 12 dog

Some shells allow you to independently redirect standard output and standard error to different files. Unfortunately, our standard interactive shell at ARE, the `tcsh', does not allow this. You can redirect standard output alone, or both standard output and error to the same file, but not to different files.

In contrast to output redirection, its also possible to use the contents of an existing file as input into a command. This is called input redirection. This form of redirection is less commonly used than output redirection. However, one useful place where it is often used is in sending a file via email to someone. Suppose you have a file `paper.tex' containing an article you have written that you want your colleague Sarah to read. This command will email her your file:

$ mailx -s "my article" sarah < paper.tex

This command redirects the contents of file `paper.tex' as input to `mailx' which packages it up, attaches a subject line and sends it to <sarah>.

Suppose you wanted to send yourself a reminder via email of some task you were suppose to do. Here is a variant of the previous example, this time using the special file `/dev/null':

$ mailx -s "water the plants today" my-username < /dev/null

The file `/dev/null' acts like an empty file in this case, the effect being you send yourself an email with the subject containing the reminder and the body of the email empty.

`/dev/null' can also be used for output redirection when you want to send all the output into a bottomless pit and never see it. Why would you ever want to do this? Well, sometimes the output from a command can be bothersome, especially if it involves error messages that you don't care about.