Linux less Command – 11 Examples of How to Use it

linux-less-command

If you’ve tried opening a long file in Linux, you’ve noticed that it fills up the terminal, which is annoying. The good news is that, with the less command, you don’t have to display the entire file’s contents on the terminal.

The less command allows you to view a file’s contents page by page, and you have control over which page to open. Besides, the less command has other options for better results. This post shares more insights on how to maximize on the less command.

What is the less Command in Linux?

The less command is one of the different utilities you can use to read the contents of a file. Although we have other options, such as using the Linux cat command or text editors, the less command stands out for allowing users to read a file’s contents page by page.

Below is the syntax for the command.

The table below presents the commonly used options when running the command.

OptionsDescription
-fForcefully open non-regular files.
-FExit the command if the file can be opened on one page.
-EExit the screen once you reach the end of the file.
-XDisplay output on screen even after exiting the command.
-sIt merges blank lines into one blank line.
-NAdds line numbers at each line.
-nRemoves line numbers when displaying the output.
-MAdds more vigorous verbose to show statistics.
-gHighlight the last match of the search string.
-iIgnore case sensitivity when running the command.
-mIt adds verbose mode by showing percentages when scrolling.
-pOpen the file at the line where the search term is first found.

We can now cover examples of using the command to get a better understanding.

How to Use the less Command in Linux

Navigating a document after opening it with the less command requires keyboard shortcuts. We’ve briefly highlighted the commonly used shortcuts and their description.

Option[Shortcuts]Description
Enter keyMove the cursor forward one line.
y or kMove the cursor backward one line.
Space key or pg-upMove forward by a page.
B or pg-downMove backward by a page.
Right arrowScroll to the right.
Left arrowScroll to the left.
Home or gMove to the start of the file.
End or GMove to the end of the file.
nMove to the next occurrence of the found string.
NMove to the previous occurrence of the search string.
/stringSearch for a string forward.
?stringSearch for a string backward.
qExit the command.

Now that you’ve understood how to navigate a file after opening it, below are practical examples of using the less command.

Example 1: Open a File Using the less Command

Opening a large file requires you to run the less command and add the filename. For instance, we can open the /var/log/kern.log as follows.

You will get a screen showing the file’s first page, and you can navigate it using the shortcuts we shared earlier.

less-command-open-file

Example 2: Include Line Numbers

Suppose you want your output to include line numbers for better readability. Add the -N option in your command.

less-command-include-line-numbers

Example 3: Search for a String

Knowing how to search for a string quickly saves time when you have a large file. For instance, we will use slash to search forwards and highlight any occurrence of the found string.

You must first open the file, then press /[string] and press enter to navigate to the first occurrence of the string.

less-command-search-for-a-string

Example 4: Retain Screen After Exiting

Suppose you want to retain the active page on the screen even after quitting the less command. Add the -X option in your command.

less-command-retain-screen

Example 5: Merge Multiple Blank Lines

The less command accepts the -s option when you want to merge multiple blank lines into a single blank line. Doing so helps minimize the number of lines on a page for better readability.

Here’s how you would run the command.

Example 6: Read a File in Real-Time

It’s possible to monitor a file for changes in real time. For instance, if working with a log file, the less command will monitor for changes and display them as they are added at the end of the file.

Use the +F option to activate this mode and navigate to view new entries by pressing the F key.

less-command-monitor-file-in-real-time

Example 7: Open File to First Occurrence of a Search String

We’ve already seen how you can search for a string in a file. However, there is another approach you can use. By adding the -p option, your file will open at the first occurrence of your search string.

Here’s an example.

linux-less-command

Example 8: Using the less Command with Multiple Files

Instead of opening one large file, it’s possible to open multiple simultaneously. To do this, run the command and add all the files you want to open.

At the end of the first page, you will notice that it shows we are currently at (file 1 of 2).

less-command-with-multiple-files

To move to the first page of the other file, press the ‘n’ key. It will now show you are at (file 2 of 2). Repeat the same depending on the number of open files you have.

less-command-open-multiple-files

Example 9: Edit the Opened File

One lovable thing about using the less command is switching the opened file to the default text editor and editing it. You only need to press ‘v’ when viewing the file, and the text editor will open.

If the file is editable, you can edit and save it. Once you exit the editor, it will take you back to the less command to continue viewing the file.

For our example, we don’t have the edit permissions. Hence, we get an error after pressing ‘v.’

less-command-edit-file

Example 10: Get a File’s Statistics

You can open a file using the -M option to view statistics about it.

less-command-show-statistics

To get more details, press the equals (=) key, and you will notice that you now get more statistics.

less-command-show-file-statistics

Example 11: Using the Command with Pipes

The pipe option is a great way to combine the less command with other commands. For this example, let’s use it with the Linux grep command to search for a string within the file.

Our command would be:

Instead of getting the entire file opened on the screen, only the matching lines are displayed because we added the grep command.

less-command-with-grep-command

Comparing the less Command with Other File Commands

The less command is mainly compared with the more command. Unlike the less command, the more command restricts how you navigate. The more command allow only scrolling forwards, yet the less command scrolls backwards and forwards.

Running the more command appears as follows.

Its output is similar to that of the less command except for the scrolling part.

more-command-vs-less-command

Another comparison is between the less and the cat command. The cat command will open the entire file on the terminal, making it hard to read as you can’t navigate.

Nonetheless, the cat command is helpful when working with small files, and you can run it as follows:

less-vs-cat-command

Conclusion

The Linux less command is handy when opening a large file. The command makes it easy to scroll through the pages and search for text. You can use different options with the command for more specific results, and this post shares practical examples to get you started. Hopefully, you’ve understood how to use the Linux less command.

Found this helpful? - Share it!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top