Redirection in UNIX

One of the most powerful features of the UNIX operating system is its ability to push information through different applications built into the system.  This feature is best demonstrated through an example.  Consider the following operation using a Macintosh: you have a text output file that consists of ten lines of unnecessary data followed by 400 lines of data organized into seven columns.  In order to perform your analysis, you must disregard the first ten lines, then take the data in the first column of each row, multiply it by the data in the fifth column and divide the result by the value in the seventh column for all rows in the file.  You must then repeat this operation for 300 different data files on the disk with a similar format.  In order to complete this operation on the Macintosh, you may choose to 1) strip the first ten lines off of all 300 files, then 2) import each text file into a spreadsheet program, 3) calculate the value needed, and 4) export these results as text in a text editor.  This operation requires that you use three different applications and repeat every operation for all 300 files in order to get at the information you need.

In UNIX, this task can be completed for all three hundred files with a single command.  This is because UNIX has the ability to take the output of a single command and instead of feeding that output to the screen, it can feed it to either another command or a file.  As a simple example, try typing the command ls -l /etc at the UNIX prompt.  Notice that the directory list goes right off of your screen, so you can't see the top of the list without scrolling back.  For long lists, the beginning would be lost because the screen couldn't hold all of the information at once.  So, a useful thing to do would be to tell the computer to give you the directory list in pieces.  We also know from the reading that the more command will display a screen full of information at a time and wait for you to hit the space bar before displaying the next screen full of information.  Wouldn't it be nice if we could tell UNIX "I want to see the contents of /etc., but I only want to see it one screenful at a time"?!  This can be done by listing the screen information and then sending all of that information to the more command.  To do this, you can use a "pipe" in UNIX which takes whatever should be going to the screen, and sends it to the command that follows the pipe.  For example, you can solve the above problem of not being able to see everything in the /etc directory at once by typing:

ls -l /etc | more

Notice that the "|" character tells UNIX to pipe the information from the ls command directly into the more command so that it will only display one screenful of information at a time.  Give this a try.

While this is a relatively simple example, you can imagine combining commands that clip the top 10 lines off of a file and commands that can perform mathematical operations on data in columns in combination to solve the problem I used as an example in which you had to perform operations on 300 different files.

In addition to piping information from one command to another, you can also write output from a command directly onto the disk instead of the screen.  For example, you may wish to save a list of your directory to the disk as a file.  To do this, you can use the ">" command which tells UNIX to take whatever it usually sends to the screen and send it to a disk file instead.  For example, the command:

ls -al /etc > example

Will list the files in the directory /etc and store them into a file named "example".  If you performed this operation again on the file named "example", the new data would overwrite the old data-- it would not be tacked onto the end of the file.  In order to append new data onto the end of an existing file, you can use the command:

ls -al /usr >> example

The ">>" tells the computer to add onto the file "example" instead of writing over it.

This should provide you with enough information to complete your Lab #3 assignment.  As a hint, keep in mind the mail command is a standard UNIX command and so it can have information piped into it from other commands....

Good luck.  If you have any questions, please email me ( or stop by my office hours and I will help you to figure it out.

Pages maintained by
Prof. Ramón Arrowsmith