Red Hat Enterprise Linux 3: Red Hat Enterprise Linux Step By Step Guide
Prev | Chapter 3. Shell Prompt Basics | Next

# 3.9. 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 with the command:

    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`, a pager utility that 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 you can view the contents of `/etc/` one screen at a time. To move forward a screen, press <KEYCAP >[Space]</KEYCAP >; to move back a screen, press <KEYCAP >[B]</KEYCAP >; to quit, press <KEYCAP >[Q]</KEYCAP >. Alternatively, you can use the arrow keys to navigate with `less`.

To search the output of a text file using `less`, press <KEYCAP >[/]</KEYCAP > and then type the keyword you want to search for within the file. For example:

    /Linux

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

    grep coffee sneakers.txt | lpr

This command prints every line in the `sneakers.txt` file that mentions the word "coffee" (read more about `grep` in Section 3.10.3 The `grep` Command).

## 3.9.1. The `more` Command

The main difference between `more` and `less` is that `less` allows backward and forward movement using the arrow keys, while `more` only uses the <KEYCAP >[Spacebar]</KEYCAP > and the <KEYCAP >[B]</KEYCAP > key for forward and backward navigation.

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

    ls -al /etc | more

Figure 3-10. Piping Output of `ls` to `more`

To search the output of a text file using `more`, press <KEYCAP >[/]</KEYCAP > and then type the keyword you want to search for within the file. For example:

    /foo

Use the <KEYCAP >[Spacebar]</KEYCAP > to move forward through the pages. Press <KEYCAP >[Q]</KEYCAP > to exit.

Prev | Home | Next
Manipulating Files with `cat` | Up | More Commands for Reading Text Files