Pipes and Pagers

A pipe is the | symbol located on the same key as the backslash on your keyboard. 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 in this chapter. 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?

As an example, 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. less is a pager utility that allows you to view information one page (or screen) at a time.

ls -al /etc | less

Now you can view the contents of /etc one screen at a time. To move forward a screen, press the spacebar; to move back a screen, press b; to quit, press q. Alternatively, you can use the arrow keys to navigate with less.


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. Use the arrow keys to navigate the file.

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

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 “The grep Command” section later in this chapter).

The more Command

The main difference between more and less is that less lets you move backward and forward using the arrow keys, while more uses the spacebar and the b key for forward and backward navigation.

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

ls -al /etc | more

Figure 8-7. Piping Output of ls to more

Use the spacebar to move forward through the pages. Press q to exit.

The Red Hat Documentation Team - Official Red Hat Linux User's Guide
The Red Hat Documentation Team - Official Red Hat Linux User's Guide
Year: 2002
Pages: 223

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net