Previous Section  < Day Day Up >  Next Section

7.11. Pipes

A pipe takes the output from the command on the left-hand side of the pipe symbol and sends it to the input of the command on the right-hand side of the pipe symbol. A pipeline can consist of more than one pipe.

The purpose of the next three commands is to count the number of people logged on (who), save the output of the command in a file (tmp), use the wc 僕 to count the number of lines in the tmp file (wc 僕), and then remove the tmp file (i.e., find the number of people logged on). See Figures 7.4 and 7.5

Example 7.41.

1   $ who > tmp

2   $ wc 僕 tmp

    4 tmp

3   $ rm tmp 



# Using a pipe saves disk space and time.

4   $ who | wc 僕

    4

5   $ du . | sort 墨 | sed 墨 '$p' 

    72388   /home/jody/ellie


Figure 7.4. The pipe.


Figure 7.5. Multiple pipes (filter).


EXPLANATION

  1. The output of the who command is redirected to the tmp file.

  2. The wc 僕 command displays the number of lines in tmp.

  3. The tmp file is removed.

  4. With the pipe facility, you can perform all three of the preceding steps in one step. The output of the who command is sent to an anonymous kernel buffer; the wc 僕 command reads from the buffer and sends its output to the screen.

  5. The output of the du command, the number of disk blocks used per directory, is piped to the sort command and sorted numerically. It is then piped to the sed command, which prints the last line of the output it receives.

    Previous Section  < Day Day Up >  Next Section