Linux Commands

Linux Less Command with Example

The Linux less command is a command-line tool that displays a file line by line or one page at a time. It is particularly helpful when viewing large files that would otherwise be cumbersome to view using a conventional text editor such as vim or nano. Less command is a bit like more command and allows you to carefully scroll through the file line by line or section by section.

In this brief guide, we feature the Linux less command alongside some command examples.

Basic Syntax

The less command takes the following syntax.

$ less [options] file

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:

$ less /etc/apache2/apache2.conf

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:

$ less -N  /etc/apache2/apache2.conf

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.

$ less -X  /etc/apache2/apache2.conf

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:

/apache

To search backward, use the question mark symbol in place of the forward slash.

?apache

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.

$ cat /etc/apache2/apache2.conf | less

This is just the equivalent of using the less command, as shown in the very first example.

$ less /etc/apache2/apache2.conf

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.

$ less --help

Summary 

That was an overview of the less command and its usage. Some good practice will come in handy to get comfortable with the command.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.