Making Output Readable with Pagers


The cat command is fine when you just want to quickly show some information, but what about large files that scroll past a bit too quickly to read and you now can't read them? There is a way to slow down the output of a command like cat, so you can read the data at your speed. Viewing pages one at a time is done with the concept of pagers. Files that scroll off the screen will be useless if you need to read the beginning of the file. If the file's size is larger than what can fit on the terminal screen or in your history buffer, you are out of luck. Files are often large enough to do this, and you will still want to read those files. You can use new Unix commands. You can use more and less. People often refer to more and less by their generic term: pagers. Let's take a quick look at how to use these command-line tools.

The more Command

All you need to do to use more is to type the command followed by the file or files you want to display. This command is similar to cat. The tricky part is memorizing when to use which and having that knowledge at your fingertips when you need it most. Let's take a look at the more command in action:

 >more hosts.allow (Output removed) 

For this command you will need to have a file that is long enough to scroll off your screen and warrants the use of the command. If you type in the wrong command, or you specify a file that is not long enough to scroll off the screen, more will more or less do nothing for you. (Pun intended.)

When you use the more command with a long file, you immediately see something new on your terminal screen. On the bottom left of the screen, you will see the more command in action:

 --More--(53%) 

Fifty-three percent is what you have seen and the rest is what still needs to be seen. You know from the percentage how many pages you are dealing with. If you had 25%, then it's three or four pages of output to view.

Pressing the Spacebar will bring you to the next page. You can use the q on the keyboard to quit the program, or Ctrl+z will also stop the more command if you want to break the sequence of using it. Using s on the keyboard will skip one line at a time and is helpful to use if you want to scroll through your output line by line, instead of page by page.

In this section we looked at how to use more to page through your output. Using cat showed you contents of a file, but you had little control over being able to view large files and this is where the more command is useful. Now, let's take a look at the less command, which is a new version of the more command. So you can do more with less!

Less Is More, Literally The meanings can actually be reversed for some computer terms, as in the case of using the commands less and more.

Using less is a newer binary that can really help you page through output better, and it is frequently used by Unix systems administrators. Therefore, it is highly probable that you already have this installed on your machine.


The less Command

Using the less command is similar to using the cat and more commands. As a matter of fact, the syntax is nearly identical. It's really what the tool does that makes it different. When using the less command, you have more control over the pager than ever before.

Here is a good example of the differences between more and less:

 >more hosts.allow 

If you try to use the arrow keys for navigation while using the more command, you will find that you can only move forward through the file with the Spacebar. There is not a back and forth as there is with the less command. Now try the same things using the less command.

 >less hosts.allow 

You can move up and down through the file using your arrow keys. You can even move to the right buffer if you are working on a terminal emulation program like telnet or secure shell. Either way, if you are connected to a Unix system and can use more or less, check them out and see what they can do for you. Other options that come with less are similar to those that come with cat and more. The Spacebar always moves you through the file via the page, and q will give you the ability to quit out of the program.

Ask for It If you are not using the less command at work and think it may be more productive for you, request it.


Now that you are comfortable with the fundamentals on how to read a file with cat, more, and less, there may be times when you only need to look at a section of a file. You may have a need to look at the bottom of a log file, or at the top of an email message header to get the source and destination addressing out of it. If you do not need to look at a complete file and just need to look at a section of it, then you can use the head and tail commands.



    SAMS Teach Yourself Unix in 10 Minutes
    Sams Teach Yourself Unix in 10 Minutes (2nd Edition)
    ISBN: 0672327643
    EAN: 2147483647
    Year: 2005
    Pages: 170

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