In this brief guide, we feature the Linux less command alongside some command examples.
The less command takes the following syntax.
Less command without any command options
The Linux less command can be executed in its basic form without any flags or options, follows by the file name. Suppose you want to view the contents of the /etc/apache2/apache2.conf configuration file.
You will run the following command:
You can scroll one line at a time by pressing the ENTER button and SPACE button to scroll one page at a time.
To return back to the terminal, simply press the ‘Q’ key on the keyboard
Display lines numbers with the less command
The Linux less command, together with the -N flag, prints the line numbers. For example, to view line numbers in the /etc/apache2/apache2.conf configuration file execute:
Leave content on the terminal after exiting
Normally, when you exit from viewing a file using less command. The output disappears. However, if you wish to leave the content on the terminal even after exiting, use the -X option as shown.
From the output shown below, you can see the output is left on the terminal.
Search for a string with less command
You can search for a string in the output by simply pressing the forward-slash key and typing the search keyword.
For example, to search for the string apache in the output displayed, execute:
To search backward, use the question mark symbol in place of the forward slash.
As observed, the string is highlighted on the screen.
Redirect output to less command using a pipe
Output from other commands can be piped to less command to make it easier to scroll along each line at a time. A perfect example is piping the cat command to less command, as shown.
This is just the equivalent of using the less command, as shown in the very first example.
Navigating around the output
Less command offers the following keyboard shortcuts to help you move around.
To move forward one-line: Press ENTER key, e or j
To move backward one line: Press y, arrow up or k
To move one window forward: Press SPACE or z
To move one window backward: Press b
To quit : Press Q
For more command options, run the command below.
That was an overview of the less command and its usage. Some good practice will come in handy to get comfortable with the command.