next up previous contents index
Next: Input Redirection Up: The Standard Input and Previous: Unix Concepts

Output Redirection

 

A very important feature of Unix is the ability to redirect output. This allows you, instead of viewing the results of a command, to save it in a file or send it directly to a printer. For instance, to redirect the output of the command ls /usr/bin, we place a > sign at the end of the line, and say what file we want the output to be put in:

screen5669

As you can see, instead of writing the names of all the files, the command created a totally new file in your home directory. Let's try to take a look at this file using the command cat. If you think back, you'll remember cat was a fairly useless command that copied what you typed (the standard input) to the terminal (the standard output). cat can also print a file to the standard output if you list the file as a parameter to cat:

screen5675

The exact output of the command ls /usr/bin appeared in the contents of listing. All well and good, although it didn't solve the original problem.gif

However, cat does do some interesting things when it's output is redirected. What does the command cat listing > newfile do? Normally, the > newfile says ``take all the output of the command and put it in newfile.'' The output of the command cat listing is the file listing. So we've invented a new (and not so efficent) method of copying files.

How about the command cat > fox? cat by itself reads in each line typed at the terminal (standard input) and prints it right back out (standard output) until it reads tex2html_wrap8266 . In this case, standard output has been redirected into the file fox. Now cat is serving as a rudimentary editor:

/home/larry# cat > fox
The quick brown fox jumps over the lazy dog.
press Ctrl-d

We've now created the file fox that contains the sentence ``The quick brown fox jumps over the lazy dog.'' One last use of the versitile cat command is to concatenate files together. cat will print out every file it was given as a parameter, one after another. So the command cat listing fox will print out the directory listing of /usr/bin, and then it will print out our silly sentence. Thus, the command cat listing fox > listandfox will create a new file containing the contents of both listing and fox.

 


next up previous contents index
Next: Input Redirection Up: The Standard Input and Previous: Unix Concepts

Converted on:
Mon Apr 1 08:59:56 EST 1996