LPI Linux Essentials Exam 010-160 - Topic 3.2 - less Pager
The less
command in Linux is designed to provide a user-friendly interface for viewing text files one screen at a time. It facilitates both forward and backward navigation through the file, setting it apart from other pager programs. Notably, less
does not require loading the entire file before displaying it, which is especially beneficial for large files.
Usage
To view a file using the less
command, simply type less
followed by the file’s name:
less filename
Alternatively, you can pipe data to less
for paging through which is useful when extracting and manipulating data:
command | less
Common Navigation Commands
The table below summarizes some of the common navigation commands available in less
:
Command | Description |
---|---|
SPACE |
Scroll forward one screen |
b |
Scroll backward one screen |
d |
Scroll forward half a screen |
u |
Scroll backward half a screen |
G |
Go to the end of the file |
g |
Go to the beginning of the file |
/pattern |
Search forward for a pattern |
?pattern |
Search backward for a pattern |
n |
Repeat the previous search |
N |
Repeat the previous search in the opposite direction |
q |
Quit less |
Common Use Cases
Viewing Large Log Files
less
is highly useful for viewing large log files, as it allows you to start viewing the file immediately without waiting for the entire file to load:
less /var/log/syslog
Searching Through Text
less
provides powerful search functionality, allowing you to search both forward and backward through a text file. After initiating a search, you can use n
and N
to navigate through the search results:
less filename
/pattern
Viewing Compressed Files
less
can directly view compressed files without the need to decompress them first:
less file.gz
Viewing Multiple Files
less
allows you to view multiple files by specifying multiple filenames when you start less
. You can then navigate between the files using the :n
and :p
commands:
less file1 file2
:n
:p
Conclusion
The less
command is an essential tool for Linux users, providing a wide range of features for viewing and navigating through text files. Whether you are dealing with large log files, searching through text, or simply reading documentation, less
offers a user-friendly and efficient solution. Its ability to display files without loading the entire content, combined with its extensive navigation and search capabilities, makes less
a go-to tool for viewing data.