One of the most powerful metacharacters
is the pipe (|). The pipe takes the standard output of one command and
passes it as standard input into a following command. In effect, pipes
enable you to build a miniature program. The pipe symbol is sometimes
referred to as a double vertical bar and is found below the backspace
key. You must always have a command on each side of a pipe.
Spaces between the commands and the pipe are optional. 
| Command Format: |
| command |
command |
Displaying Command Output One Screen
at a Time
One of the most common uses of the pipe metacharacter is to send the
output of a ls
command to the more
command to enable you to see a long directory listing one screen at a
time. The first example in Figure
shows this basic use of the pipe. The standard output from the command
to the left of the pipe becomes the standard input for the command on
the right side of the pipe.
By rolling your mouse over Figure ,
you can view explanations for the multiple commands interconnected by multiple
pipes. Several commands are introduced here that you will work with
later in the course.
The second example in Figure
uses the ps
-ef command to display a detailed
list of all processes running on the system, and that output is passed
as input to the grep
command, which searches for the string "brad" in all
processes. The resulting output of the grep
command is passed as input to the sort
command, which sorts the output using the Process ID (PID). The sorted
output then becomes input to the tail
command, which selects only the last 10 lines of output. That output is
then sent to the default line printer lp.
 |
|
Interactive Media Activity
(Flash, 95 kB) |
| |
Command
Piping
In this media activity, you are logged in as user2 and your current working directory is: /home/user2/dir2. Refer to the Class File Tree Structure by clicking the 'tree' button located on the Menu Bar and type the commands that would accomplish the requested objectives.
Note: Be sure to press enter after each one. Click on step 1 to begin.
|
|
|
|
 |
 |
Lab
Activity
(Adobe
Acrobat Reader, 45 kB) |
| |
Redirection
and Piping In
this lab, you will use advanced UNIX commands to
accomplish redirection and piping. |
|
|
|
|