< Previous | Contents | Next >
Viewing File Contents With less
The less command is a program to view text files. Throughout our Linux system, there are many files that contain human-readable text. The less program provides a conve- nient way to examine them.
What Is “Text”?
There are many ways to represent information on a computer. All methods in- volve defining a relationship between the information and some numbers that will be used to represent it. Computers, after all, only understand numbers and all data is converted to numeric representation.
Some of these representation systems are very complex (such as compressed video files), while others are rather simple. One of the earliest and simplest is
called ASCII text. ASCII (pronounced "As-Key") is short for American Standard Code for Information Interchange. This is a simple encoding scheme that was first used on Teletype machines to map keyboard characters to numbers.
Text is a simple one-to-one mapping of characters to numbers. It is very compact. Fifty characters of text translates to fifty bytes of data. It is important to under- stand that text only contains a simple mapping of characters to numbers. It is not the same as a word processor document such as one created by Microsoft Word or LibreOffice Writer. Those files, in contrast to simple ASCII text, contain many non-text elements that are used to describe its structure and formatting. Plain ASCII text files contain only the characters themselves and a few rudimentary control codes like tabs, carriage returns and line feeds.
Throughout a Linux system, many files are stored in text format and there are many Linux tools that work with text files. Even Windows recognizes the impor- tance of this format. The well-known NOTEPAD.EXE program is an editor for plain ASCII text files.
Why would we want to examine text files? Because many of the files that contain system settings (called configuration files) are stored in this format, and being able to read them gives us insight about how the system works. In addition, some of the actual programs that the system uses (called scripts) are stored in this format. In later chapters, we will learn how to edit text files in order to modify systems settings and write our own scripts, but for now we will just look at their contents.
The less command is used like this:
less filename
less filename
Once started, the less program allows us to scroll forward and backward through a text file. For example, to examine the file that defines all the system's user accounts, enter the following command:
[me@linuxbox ~]$ less /etc/passwd
[me@linuxbox ~]$ less /etc/passwd
Once the less program starts, we can view the contents of the file. If the file is longer than one page, we can scroll up and down. To exit less, press the “q” key.
Viewing File Contents With less
The table below lists the most common keyboard commands used by less.
Table 3-3: less Commands
Command Action
Command Action
Page Up or b Scroll back one page Page Down or space Scroll forward one page Up Arrow Scroll up one line
Down Arrow Scroll down one line
G Move to the end of the text file
1G or g Move to the beginning of the text file
/characters Search forward to the next occurrence of characters
n Search for the next occurrence of the previous search
h Display help screen
q Quit less
Less Is More
The less program was designed as an improved replacement of an earlier Unix program called more. The name “less” is a play on the phrase “less is more” — a motto of modernist architects and designers.
less falls into the class of programs called “pagers,” programs that allow the easy viewing of long text documents in a page by page manner. Whereas the more program could only page forward, the less program allows paging both forward and backward and has many other features as well.