The “less” Command in Linux [10 Practical Examples]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

The less command in Linux is a frequently used command to view any file, though it doesn’t provide an editing opportunity. It helps viewers to see a document page by page and gives better control to navigate through the file.

A. Description

less is a handy command in Linux that shows any file’s contents one page at a time. This command is more useful when viewing a large file with many lines because it doesn’t load the entire file, so it gives a first loading speed.

Note: You can navigate through the texts using the ARROW keys and navigate page to page using the SPACE key.

B. Syntax

The syntax of less command in Linux  is very simple which is as below:

less [OPTIONS]... (FILE_PATH OR FILE_NAME)

Note: In the syntax above square bracket means the OPTIONS are not mandatory and three dots after the square bracket means multiple OPTIONS can be used after the less command.

C. Options

There are many useful options available for the less command. I have listed some of the most used options which are given below. You can know more about options and their use by checking the man page.

man less

Useful Options

  • -E: To take the automatic exit after reaching the end of a file.
  • -f: Use to force less to open non-regular files.
  • -F: Open the file in the terminal when the entire file can be displayed on the first screen.
  • -i: Ignores case sensitivity
  • -n: Remove the line number from the screen.
  • -N: Display the line number at the beginning of each line.
  • -p[pattern]: Start the file from the line that contains the pattern for the first time.
  • -s: Marge multiple spaces in one space.
  • z[n]: Change the scrolling window size to n line.

NB: The options in Linux CLI (Command Line Interface) are all case-sensitive, So be cautious while using them.

Practical Examples of the “less” Command in Linux

less is a helpful command that helps to show a document in a manageable way. In the section below I will show you some most useful applications with less command in Linux.

Example 1: Open a Text File With the “less” Command in Linux

You can view a file’s contents page by page by opening that file with less command in Linux. For this, you can use a relative path or an absolute path of the file location. Here, I have a file named file.txt under the location /home/amdadul. Now, I will display this file page by page. To do so you can follow the below procedure.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

 Type the following command in the command prompt:

less file.txt

➌ Now, press the ENTER button.

Output >

In the following image, you can see that less has displayed the contents of file.txt on one page at a time format.Open a Text File named file.txt With the “less” Command in Linux

Example 2: Open a Text File With Line Number

To display the line number of a specified file you can use the -N option after less command. Here I will show you the file.txt with the line number and to do so you can follow me in the manner I describe below.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

 Type the following command in the command prompt:

less -N file.txt

➌ Now, press the ENTER button.

Output >

In the image below, you can see the file’s contents with line numbers.A file open with line number by less command in linux

Example 3: Search for a String With the “less” Command in Linux

You can search for a specific pattern under the terminal of less command. /PATTERN is used to search forward from the current position for the PATTERN string and ?PATTERN is used to search backward from the current position for the PATTERN string. You will see the search phrase at the bottom line of the less terminal display. Now,  I will show you the process to search for something, to do so you can follow me in the manner I describe below.

Note: Searching in less is case-sensitive. You can ignore case sensitivity by using the -I option or pressing the I key within less.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

 Type the following command in the command prompt:

less -I file.txt

➌ Now, press the ENTER button. After pressing ENTER the file will open and you will see the search phrase at the bottom as in the image below.

➍ Type the string which you want to search in the search phrase. Here, I will search for the string “book.Search phrase in less command terminal.➎ Now, press the ENTER button.

Output >

When I search for the string book the result I get as the image below. You will get the same for your desired search.Search results obtained from searching in less terminal.

Example 4: Open a File Starting With a Specific Pattern

If you want to open a file from a line that contains a specific PATTERN for the first time in the document then you can use the -p option before the desired PATTERN. I am adding an example that you can follow according to the process below

Steps to Follow >

➊ At first open the Ubuntu Terminal.

 Type the following command in the command prompt:

less -p Book file.txt

➌ Now, press the ENTER button.

Output >

Through this process, you will get the result as shown in the picture below. Here, you can see that the file opens at the line where the Book string is used for the first time in the document.Opening a file from a line with a specific pattern

Note: The search is case-sensitive.

Example 5: Remove Multiple Blank Lines

To remove multiple-line space in a document at the time of reading with less command in Linux, you can use option -s with the less command. Here I will show you an example with file2.txt that contains a multiple-line gap between two paragraphs. Without using option -s the file2.txt looks like as below.Document with multiple blank lines. The steps to remove multiple spaces are as follows.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

 Type the following command in the command prompt:

less -s file.txt

➌ Now, press the ENTER button.

Output >

Now you can see, there is no unnecessary space between two lines. It helps to show more lines on one page.Blank lines were removed after using the -s option.

Example 6: Open Multiple Files With the “less” Command in Linux

The less command can open multiple files at a time and can show them individually. In this section, I will show you, how to open multiple files together. Let’s dive into the example.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

 Type the following command in the command prompt:

less file.txt file3.txt

➌ Now, press the ENTER button.

Output >

Two files are opened together. You can move the next file by pressing “: (SHIFT+;) & N”The first document is opened together with another file by less command.After pressing “: and N”The second document is opened together with another file by less command.To move the previous file you have to press  “: & P”

Example 7: Marking a Specific Text With “less” Command in Linux

Mainly mark is a flag. You can mark any section of a large document so that you can return to that section at any time from any part of the document. The process you can flow through is given below.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

 Type the following command in the command prompt:

less file.txt

➌ Now, press the ENTER button.

➍ Press M (which initiates the marking process) followed by a letter of your choice. To add more marks, use different letters.

Output >

You will see the following interface while initiating the marking process. Now, press any key to make that as a mark for this section; consider that you use the letter “a” as a mark.Showing the initiation of marking process. Now you can go back to this section from any section of this document by pressing followed by the letter you choose as a mark (for this example ‘a’ is the letter). In this case, you have to type ” ‘a “.

Example 8: Real-Time Monitoring With “less” Command in Linux

The +F option with the less command in Linux allows you to monitor in real-time. less can show you the most recent messages or lines that have been added to a file in real-time by using the +F option. In the section below, I am showing you an example so that you can get a better understanding.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

 Type the following command in the command prompt:

less +F file.txt

➌ Now, press the ENTER button.

Output >

You can see in the image, less indicates that it is awaiting additional data by displaying a message. For new messages, the terminal automatically scrolls down.Real-Time monitoring interface in less terminal. By using Ctrl+C, you can exit forward mode and go back to the standard less interactive mode.

Note: In the forward mode, scrolling and paging are disabled because less only shows the bottom of the output while it waits for new messages.

Example 9: Redirecting to the “less” Command in Linux

You can use the pipe to process the output of other commands via less. It is very useful in day-to-day life. Here I will show you an example so that you can follow the manner I describe below.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

 Type the following command in the command prompt:

sudo dmesg  | less

Notes: In Linux, the sudo (Super User DO) command is frequently used as a prefix to a command that only superusers are permitted to execute.

➌ Now, press the ENTER button.

Output >

The dmesg command typically produces thousands of lines of output. You can read this output more comfortably if you pipe it to the less.Pipe the output of dmesg command to the less command.

Example 10: Edit Files with the “less” Command in Linux

less provides simply the ability to see files, but it makes up for this with a shortcut that allows you to modify files. Press v when reading a file in less to move it to the system’s built-in text editor (nano is Ubuntu’s default text editor). The file will automatically reopen in less after closing the editor.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

 Type the following command in the command prompt:

less file.txt

➌ Now, press the ENTER button. It will open the file on the less terminal page.A file opened in less terminal.➍ Finally, press the V key

Output >

The file.txt now opens with the system’s built-in text editor nano. Now, You are able to edit the file.txt as you want.The file in the less terminal shifted to the nano terminal after pressing 'V'.

Conclusion

I’ve demonstrated the operation of the less command in this article. Additionally, I have included some helpful examples. If you carefully read this article, you should be able to use the command less, effectively on your own.


Similar Readings

Rate this post
S. M. Amdadul Islam

Hello everyone. I am S. M. Amdadul Islam, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Naval Architecture and Marine Engineering (NAME) graduate from the Bangladesh University of Engineering and Technology. In addition to my regular work, I enjoy watching Youtube, discovering new things, gossiping with friends, visiting new places, and taking photos of landscapes. Read Full Bio

Leave a Comment