Полезная информация

Next Up Previous Contents Index
Pipes

2.10 Pipes

No, we're not going to start talking about plumbing here. In Linux, ``pipes'' connect the standard output of one command to the standard input of another command.

Let's take a step back, to the ls command. There are plenty of options available with ls, but what if the contents of a directory stream by too quickly for you to view them?

Let's view the contents of the /etc directory.

ls -al /etc

How do we take a closer look at the output before it races off the screen?

One answer is to pipe the output to a utility called less. Known as a pager, less, (like more) allows us to view information one page (or screen) at a time.

We use the vertical bar (|) to pipe the commands (as shown in Figure 34).

ls -al /etc | less

Now we can view the contents one screen at a time. To move forward a screen, just press [Space]; to move back a screen, press [B]; to quit, just press [Q].

Figure 34: Piping the output of ls to the less pager

Actually, we've already been using pipes, before we even discussed what they were.

In previous references to man pages, we used the following to print out the man page entry:

man ls | col -b | lpr

Here, we're sending the output of man ls to a filter called col with an option of -b to help format the text for the printer, then we sent the output of that to the printer using the lpr command.

  • Summary: Piping allows you to send the output of one command as the input of another command. For example: ls -al /etc | more pipes the output of the ls command to the more utility for easy viewing.


  • Next Up Previous Contents Index