Using a pipe
The output from one command can be piped in as
input to the next command. Two or more commands linked by a pipe (|)
are called a pipeline. A pipeline is written
as:
command | command | ...
You enter
the commands on the same line and separate them by the "or-bar" character |
. Many commands are well suited
to being used in a pipeline. For example, the grep command
searches for a particular string in input from a file or standard
input (the keyboard). A command such as:
history | grep "cp"
displays
all the cp commands recorded among the 16 most recently recorded
commands in your history file. The command:
ls –l | grep "Jan"
uses ls to obtain information about the contents of
the working directory and uses grep to search through this
information and display only the lines that contain the string Jan
.
The pipeline displays the files that were last changed in January. A filter is a command that can read from
standard input and write to standard output. A filter is often used
within a pipeline. In the following example, grep is the
filter:
ps -e | grep cc | wc -l
lists all your
processes that are currently active in the system and pipes the output
to grep, which searches for every instance of the string cc.
The output from grep is then piped to wc, which
counts every line in which the string cc occurs and sends
the number of lines to standard output.