Pipes

No, we're not 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 Figure 3-15).

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 3-15. Piping Output of ls to less

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 man page entries:

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.