Solaris Advanced User's Guide

Redirecting and Piping Command Output

Unless you indicate otherwise, commands normally display their results on the screen. Some special symbols allow you to redirect the output of a command. For example, you might want to save the output to a file rather than display it on the screen. The following example illustrates the use of the redirect symbol (>).


$ date > sample.file
$ 

In this example, the output from the date command is redirected to a new file called sample.file. You can display the contents of sample.file by typing the more command.


$ more sample.file
Tue Oct 31 15:34:45 MST 2000
$

As you can see, the contents of sample.file now contain the output from the date command. See Chapter 3, Working With Files and Directories for information on the more command.

Sometimes you might want to redirect the output of one command as input to another command. A set of commands strung together in this way is called a pipeline. The symbol for this type of redirection is a vertical bar (|) called a pipe.

For example, instead of saving the output of a command to a file, you might want to direct it as input to the command for printing (lp) by using the pipe symbol (|). To send the output from the date command directly to the printer, type the following:


$ date | lp
request id is jetprint-46 (1 file)
$

This pipeline would print the results of the date command. See Submitting Print Requests to the Default Printer for information on using the lp command to print files.

The command redirection examples shown here are simple, but when you learn more advanced commands, you will find many uses for piping and redirection.