Combining commands (piping)
Unix supports and is actually based upon the ability to connect commands to
one another. By piping commands together, you can use the output of one command
as the input for another.
The pipe symbol is a vertical bar (|). To pipe commands together, you
simply place a pipe between each command on the command line. For example
command1 | command2 | command3
Although you can pipe numerous commands together, you'll rarely need to
do so.
Here are some examples you can learn from and pilfer for your own uses.
If you don't completely understand what the commands are that are being
piped, look them up in other parts of this tutorial.
Example: ls -alFs | more
This command sends the output of the ls command with
the options alFs to the more command. The options
used with ls indicate that everything will be listed,
in long form, marking directories with a slash (/), executables (programs)
with an asterisk (*), and listing the size in kilobytes of each file. Normally
such a list would scroll by too quickly to view, but by piping the output
through the more command you can view one page at a time.
Example: zwho | wc -l
This command takes the output from the zwho command,
which lists everyone currently logged in to the system, and passes the
information to the wc command, which, using the -l option,
counts the number of lines that the zwho command generated.
Since zwho generates a line for each person logged in,
the number of lines is equal to the number of people logged in to the system.
Example: zwho | grep Chris
Similar to the first example, this command pipes the output of the zwho command
to the grep command, which searches for instances of the
string of letters "Chris". If grep finds a line containing
the letters, grep prints the line(s) to the screen.
Example: zwho | grep Chris > List_of_chris
You can combine piping with redirection to create files which contain
the output. In the example above, the list of lines from the zwho command
containing the letters "Chris" is redirected from the screen (the standard
output device) to the file "List_of_chris". You could view the resulting
file with a viewer, such as the more command, or an editor
such a Nedit.
Content last updated May, 2004, by daniel
Page last modified
July 20, 2004
by cawalker
|