less Command in Linux | Explained

In Linux, various command line utilities are exercised to get the content of the text files, i.e., cat, less, more, etc. Among these,  the “less” command displays one screen of content at one time. The further content is hidden and can be viewed line by line by using the return key. In the following guide, the introduction, working, and purpose of the less command are illustrated in detail.

The outline of this guide is listed below:

What is less Command in Linux?

In Linux, the “less” command displays the file content page by page, allowing users to read the content more easily. The working of the “less” command depends on the following syntax:

Syntax:

The basic syntax of the “less” command is written below:

$ less [options] file

In the syntax, “less” is the main keyword, the square brackets show the supported “options” of the “less” command and “file” shows the specific text file.

The important supported options of the “less” command are listed in the below table:

OptionsDescription
-NDisplays the numbering at the beginning of each line in the file.
-GRemoves all the highlighted strings found using the search.
-EMove to the end of the entire file.
-FExit the less command if it is displayed on the first screen.
-gHighlights the last found string.
-fForce the less command to open a file.
-nRemoves the numbering from the content of the entire file.
-iIgnore the case sensitivity.
-XPrevents the clearing of the screen after exiting from the less command.
-sRemoves all the extra spaces between the lines and paragraphs.

To get more options, execute the following help command on the terminal:

$ less --help

Press the “Return” to get more options otherwise, press “q” to quit from the “less –help” command.

How to Use less Command in Linux?

The purpose of the “less” command is to represent the specific file content at a time, page by page. Moreover, it contains a list of supported options that shows the desired output. This section demonstrates the practical implementation of the “less” command:

Example 1: Open the Specific Text File

Using the “less [File name]” option of the command, you will get all the content of that file in an organized manner. Usually, this variant of the “less” command is used the most. Here a text file is open as an example using the command:

$ less File.txt

The output displays the content of the “File.txt” that can only be read.

Example 2: Numbering the File Content

The “-N” option of the “less” command displays the particular file content in line numbers. This format helps the user to search out the important points easily from the file as compared to searching in the paragraph:

$ less -N Firstfile2.txt

The output shows that the lie numbers are displayed before each line.

Example 3: Search the Particular Word or String

It is difficult to find a string or word in a very huge file. To make this process easier, use the “/[Specific patern]” option of the “less” command. This option will search the specific pattern and highlight all the matches from the file’s content. For this purpose, open the specific file with the help of the “less” command:

$ less Firstfile2.txt

Type the particular search pattern at the highlighted place in the screenshot:

In this section, the pattern “/Linux” was searched, and the output displays all the matches of “Linux” in the file:

Same procedure is followed for the searched string “ed” in the file as shown below:

The searched word is highlighted in the terminal.

Example 4: Remove Empty Spaces

The “-s” option of the “less” command is beneficial for removing all the empty spaces from the file. In our case, the “file3.txt” file contains some empty spaces between the paragraphs, as shown in the screenshot:

Type the following command on the terminal that will remove the empty spaces from the file “file3.txt”:

$ less -s file3.txt

Example 5: View Multiple Files

The user can review the content of more than one file at a time with the help of the “less” command. For this purpose, first, open the multiple files and type the name of specific files one after another using the following command:

$ less file1.txt file3.txt

As you can see that both files are opened successfully in the terminal:

Press the “n” key to move to the next file, i.e. “file3.txt”:

Press the “p” key to move to the previous file, i.e., “file1.txt”.

Example 6: Edit the Content of a File

The “-v” option transfers the file’s content into the default text editor of Ubuntu “nano”. The “nano” text editor allows the user to edit the content of a particular file according to the requirements. In this section “file1.txt” file is taken as an example:

$ less -v file1.txt

The current nano editor windows contain four sections that are described below:

  • The top line shows the program version, edited file name, and modified filename.
  • Next is the main editor window that shows the file being edited.
  • The third line is the status line which shows the important messages.
  • The last section provides the most commonly used shortcuts in the editor.

less Command Shortcuts

Here, some shortcuts are listed below that are commonly used to navigate the file content displayed by the less:

ShortcutsDescription
Up Arrow  or  y or kHelps to move on the previous line.
Down Arrow or Enter or e or jHelps to move on next line.
Page Up,  or bMove to the previous page.
Space bar or Page DownMove to the next page.
Right ArrowScroll file content to the right.
Left ArrowScroll file content to the left.
Home,g / End, G Navigate to the “start (Home,g)” / “End (End, G)” of the file.
/[string]Used to search forward matching lines.
?[string]Used to search backward matching lines.
nMove to the next search match.
NMove to the previous search match.
qQuit from the less command.

That’s all about this guide

Conclusion

The “less” command displays limited content of the file at one time which ultimately allows the user to read the content more effectively. The basic syntax of the Linux “less” command is “$ less [options] file”. This guide has briefly described the usage and working of the “less” command with various practical examples.