Pipes and Pagers

In Linux, pipes connect the standard output of one command to the standard input of another command.

Consider the ls command that was discussed earlier. There are plenty of options available with ls, but what if the contents of a directory scroll by too quickly for you to view them?

View the contents of the /etc directory.

ls -al /etc

How do you get a closer look at the output before it moves off the screen?

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

Use the vertical bar (|) to pipe the commands.

ls -al /etc | less

Now we can view the contents of one /etc screen at a time. To move forward a screen, press [Space]; to move back a screen, press [b]; to quit, press [q]. You can use the [left] and [right] arrow keys to navigate as well.


To read startup messages more closely, at a shell prompt, type dmesg | less. You will be able to read the file one screen at a time. To move forward, press the [Spacebar]; to quit, press [Q].

Pipes can also be used to print only certain lines from a file. Type:

grep coffee sneakers.txt | lpr

This will print every line in the sneakers.txt file that mentions the word "coffee" (read more about grep in the Section called The grep Command).

The more Command

The main difference between more and less is that more only lets you move forward through a file and less lets you move backward and forward.

List the contents of the /etc directory using ls and more.

	  ls -al /etc | more

Figure 11-8. Piping Output of ls to more

Use the [Spacebar] to move forward through the pages. Press [q] to exit.