This Output Is Going to Havana: Redirection
When you use a UNIX command like ls , the result (or output) of the command is displayed on-screen. The standard place, in fact, for the output of most UNIX commands is the screen. The output even has a name: standard output. As you can imagine, there is also standard input, usually the keyboard. You type a command; if it needs more input, you type that, too. The result is output displayed on-screen — all very natural.You can pervert this natural order by redirecting the input or output of a program. A better word is hijacking. You say to UNIX, “Don’t display this output on-screen — instead, put it somewhere else.” Or, “The input for this program is not coming from the keyboard this time — look for it somewhere else.”The “somewhere else” can be any of these sources:
A file: You can store the output of ls (your directory listing) in a file, for example.
The printer: It’s useful only for output. Getting input from a printer is a losing battle.
Another program: This one gets really interesting when you take the output from one program and feed it to another program!
A bunch of UNIX programs are designed primarily to use input from a source other than the keyboard and to output stuff to someplace other than the screen. These kinds of programs are called filters. Readers old enough to remember what cigarettes are may recall that the advanced ones had a filter between the cigarette and your mouth to make the smoke smoother, mellower, and more sophisticated. UNIX filters work in much the same way, except that they usually aren’t made of asbestos.Tip The only exception to this redirection business is with programs, such as text editors and spreadsheets, that take over the entire screen. Although you can redirect their output to the printer, for example, you won’t like the results (nor will your coworkers, as they wait for a pile of your garbage pages to come out of the printer). Full-screen programs write all sorts of special glop (they give instructions) to the screen to control where stuff displays, what color to use, and so on. These instructions don’t work on the printer because printers use their own, different kind of glop. The short form of this tip is that redirection and editors don’t mix.
Grabbing output
So how do you use this neat redirection stuff, you ask? Naturally, UNIX does it with funny characters. The two characters < and > are used for redirecting input and output to and from files and to the printer. You use another character (| ) to redirect the output of one program to the input of another program.To redirect (or snag, in technical parlance) the output of a command, use > . Think of this symbol as a tiny funnel into which the output is pouring (hey, we use any gimmick we can to remember which funny character is which). To make a file called list.of.files that contains your directory listing, for example, type this line:
ls > list.of.files
UNIX creates a new file, called list.of.files in this case, and puts the output of the ls command into it.
Warning If list.of.files already exists, UNIX blows away the old version of the file. If you don’t want to erase the existing file, you can tell UNIX to add this new information to the end of it (append the new information to the existing information). To do it, type this line:
ls >> list.of.files
The double >> symbol makes the command append the output of ls to the list.of.files file, if it already exists. If list.of.files doesn’t exist already, ls creates it.Tip Some (but not all, of course) versions of the C shell check to see whether the file already exists and refuse to let you wreck an existing file with redirection. To overwrite the file if your C shell works this way, use rm to get rid of the old version. The command that tells the C shell not to clobber an existing file when you’re creating a new file from redirection is set noclobber . To turn this protection off, you can use the unset noclobber command. We recommend turning on noclobber every time you run UNIX (or get a UNIX wizard to help you make this command execute automagically every time UNIX starts up).
Redirecting input
Redirecting input is useful less often than redirecting output, and we can’t think of a single, simple example in which you would want to use it. Suffice it to say that you redirect input just like you redirect output except that you use the < character rather than the > character.