less - more than more.

One of the more common activities you will perform in the Unix environment is examining the contents of a file or a stream of text output from a command.

The Unix system recognizes several types of files and provides the appropriate utilities for viewing. The files you will most often look at are files known as regular files. Regular files come in two varieties, text file and binary files.

Text files use the standard ASCII character set (binary values 1 to 127) and are considered human viewable, although some of these characters represent formatting such as tabs, line feeds, or even a bell and the actual contents may have no syntactical or symantical meaning. Examples of text files would be program source code, shell scripts, most configuration files, and some data files.

Many newer operating systems and applications are replacing ASCII with a character set known as Unicode. For now we will assume ASCII as the default and point out situations where the ASCII/Unicode issue has an influence.

Binary files contain bytes of data with values in the full binary range 0-255. Values above 127 are treated as non-displayable values. If viewed directly from the terminal, they can cause the terminal to misbehave or lock up. An example of a binary file would be a compiled executable programs.

Unix provides a variety of utilities for viewing the contents of a file and some of these can safely view binary files.

We will examine several of these in different modules. However, in this module we concentrate on less.

less is probably the most used file viewer around. Originally written by Mark Nudelman in the mid-80s as a replacement for more, it is now supported as one of the GNU utilities and is available for Linux, most other Unix systems, and even the dos/Windows environments.

less allows you to view any regular file you have read access to. While viewing a file's contents you may move either forward toward the end of the file or backwards toward the top of the file, a feature not available in "more". If the file contains binary data, less will substitute non-printable characters with generic characters often displayed in reverse highlight.

Experiment :

#( To see this on our Linux box, try the following at the prompt : )

less /usr/bin/less

#( If less warns you about viewing a binary file : )


  "/usr/bin/less" may be a binary file.  See it anyway?

( enter y )

( You will then see the contents of the file. Use the [up] and [down] arrow or j and k keys to scroll down and up. When you are done viewing the binary code for the less program, type q to quit. )

( To view ASCII text, in this case as output from a command, run the following command sequence : )

set | less

( This will send the output of the set command to less and allow you to view it. This is about 600 lines of text on our system and you can experiment with the various "less" commands we discuss. )

less allows you to scroll down through a file one line at a time using the j key, the [down] arrow key, or the [enter] key. You can also scroll back up with the k key or the [up] arrow key. Remember, q exits less.

If you precede the movement keystroke with a numeric value, it will multiply that keystroke by that amount. So, 8j will move down 8 lines.

d and u move down or up half a screen. f and b move down (forward) or up (back) one screen.

Since it is possible to have a text file containing lines with more characters than the screen width, you can use the [left] and [right] arrow keys to shift the display left or right. If all lines fit in the screen, this will have no affect.

To move to the bottom of the file being viewed, used G (capitalized). To move back up to the top, use g (lower case).

To see a quick help or list of all the less commands, type h. In the quick help, use j to scroll down, k to scroll back up, and q to quit the help.

less provides a search mechanism. While viewing a file, type / This will move the cursor to the bottom of the display. Type in the text you wish to locate and press [enter]. less will search forward starting from the current lines being displayed. If a match is found, it will scroll down the file, moving the line containing the 1st match to the top of the screen and inverting the matching text to highlight. less will highlight any additional matches on the current display. You may move the next match to the top of the display by typing / again without entering any text and pressing . less will repeat the scroll and highlight as long as you repeat the search until the end of the file is encountered.

To search back up the file (toward the top), use ? instead of /. If you have already stated the search string with /, you can just type ? to search back.

Depending on the configuration of your shell (command interpreter), a search you initialize may or may not be case sensitive.

You may use regular expressions with the search mechanism to generalize or qualify the string to be found. We will look at regular expressions in detail in a later module. But the following is a quick demonstration of the functionality of regular expressions.

For example :

/START

searches for the string "START". It will match the string even if is a part of another word such as AUTOSTART

/\<START

searches for the string "START" but only if it is the beginning of a word, so it will not match AUTOSTART but would match StartupMaps.

We will look at regular expressions in great detail in a later module.

One final feature we will look at is viewing multiple files in a single less session. If you are running less from the examples above, quit. To view multiple files, invoke less with a list of filenames. Use the follow on our system :

less /etc/profile /etc/bash.bashrc

The first file listed should be the file displayed. You may confirm this by looking at the bottom line of the display. This line should list the current file, the number of files buffered, number of the lines currently being viewed file and the total number of lines in the file, and possibly, the next file in less's the buffer.

To display the next document listed, enter :

:n

You must use the colon but you won't have to press . If you specified more that two files to view at the same time, you may issue the command again. Once you are viewing the last file listed on the command line you cannot go any further. However you may go to the previously viewed file with

:p

Again, repeat this until you are displaying the 1st file listed.

If you to terminate the use of less, issue the q (quit) command. If you wish to only delete the currently viewed file from the list of files being viewed, use :

:d

This will not delete the file from the file-system. less will never alter a file being viewed.

The less commands covered in this module should get you started with less. Take the time to look at the man page and the internal help screen and play with less until you are comfortable.

The man help system