Redirection and pipes

Many Unix commands expect input from the keyboard and output to the screen. Thus we have "standard input" and "standard output". Often, however, we prefer that the input comes from a file, instead of the keyboard, or the output goes to a file, instead of the screen. Thus we divert or "redirect" the input or output. We can also chain commands in such a way that the output of one becomes the input to the next. This construct is called a "pipe".

Output redirection

For example to get the current time and date and put it in a file called "today" we use

	date > today
The right angle ">" (think of it as the head of an arrow) points in the direction of the flow of information, namely from the command into the file. It specifies that what ordinarily would go to the screen should instead go to the file "today". Try this command and then list the file with
     cat today

Note that with the course default protection, you can't redirect to an existing file. (That keeps you from inadvertently destroying valuable files.)

Appending by redirection

The double right angle "»" appends to an existing file. Thus

	date >> today
appends today's date to the file "today", provided it already exists.

Input redirection

The left angle "<", on the other hand, is used to specify that input comes from an existing file, rather than from a keyboard.

Here is an example. Suppose you wanted to mail a file to someone as the text of the mail message and not as an attachment. Suppose you have a file called my_first_novel.txt and your friend's user name is joshua. The command is

       mail -s "my first novel" joshua < my_first_novel.txt

The header of every mail message has a subject field to help the addressee classify your message. The mail option -s "my first novel" causes the subject field to be "my first novel". The quotes are necessary, here. The next field joshua is the user name of the addressee. In place of keyboard input for the text of the message, input is taken by redirection from the file my_first_novel.txt. The message goes out immediately without further additions.


This extremely useful feature makes it possible to chain together a series of Unix commands so that the output of one becomes the input to the next. The first practical example you will encounter in this course is with the course plotting utility "axis". To plot a file of x-y values, called "data", use the command

	axis < data | xplot

Notice that input to the command "axis" is redirected from the file "data". The pipe symbol, the vertical bar "|", signifies that the output from the "axis" command is taken to be input to the "xplot" command. Output from the "xplot" command, in turn, is the actual plot, appearing in a separate window on your screen. Why, might you ask, have two commands where one will do? The answer goes back to a time-tested Unix precept: keep the commands simple and modular, like building blocks. The "axis" command produces output in a generic format that is recognized by other software. Thus by putting together different building blocks we can get different results:

	axis < data | psplot >

is the way to generate a Postscript file for creating a hardcopy version of the plot. In this case "xplot" is replaced by "psplot", proprietary software from Adobe, a utility that converts the generic plot format into a Postscript file (note the redirection to the file ""), suitable for printing on a Postscript printer.