home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Book HomeLearning UnixSearch this book

3.2. Looking Inside Files with less

By now, you're probably tired of looking at files from the outside. It's kind of like going to a bookstore and looking at the covers, but never getting to read a word. Let's look at a program for reading files.

If you want to "read" a long file on the screen, your system may have the less command to display one "page" (a terminal filled from top to bottom) of text at a time.

If you don't have less, you'll probably have similar programs named more or pg. (In fact, the name less is a play on the name of more, which came first.) The syntax is:

less option(s) file(s)

less lets you move forward or backward in the files by any number of pages or lines; you can also move back and forth between two or more files specified on the command line. When you invoke less, the first "page" of the file appears. A prompt appears at the bottom of the terminal (or terminal window), as in the following example:

$ less ch03
A file is the unit of storage in Unix, as in most other systems.
A file can hold anything: text (a report you're writing,
	.
	.
	.
:

The basic less prompt is just a colon (:)--although, for the first screenful, less displays the file's name as a prompt. The cursor sits to the right of this prompt as a signal for you to enter a less command to tell less what to do.

Like almost everything about less, the prompt can be customized. For example, using the less -M option on the less command line makes the prompt show the filename and your position in the file. (If you want this to happen every time you use less, you can set the LESS environment variable to M (without a dash) in your shell setup file. See Section 3.7, later in this chapter.)

You can set or unset most options temporarily from the less prompt. For instance, if you have the short less prompt (a colon), you can enter -M while less is running. less responds "Long prompt (press RETURN)," and for the rest of the session, less prompts with the filename, line number, and percentage of the file viewed.

To display the less commands and options available on your system, press "h" (for "help") while less is running. Table 3-1 lists some simple (but still quite useful) commands.

Table 3-1. Useful less commands

Command

Description

Command

Description

SPACE

Display next page.

v

v

RETURN

Display next line.

CTRL-L

Redisplay current page.

nf

Move forward n lines.

Help.

Move backward one page.

:n

Go to next file on command line.

nb

Move backward n lines.

:p

Go back to previous file on command line.

/word

Search forward for word.

q

Quit less.

?word

Search backward for word.

   



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.