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.
$ less [options] [file]
The table below presents the commonly used options when running the command.
Options | Description |
-f | Forcefully open non-regular files. |
-F | Exit the command if the file can be opened on one page. |
-E | Exit the screen once you reach the end of the file. |
-X | Display output on screen even after exiting the command. |
-s | It merges blank lines into one blank line. |
-N | Adds line numbers at each line. |
-n | Removes line numbers when displaying the output. |
-M | Adds more vigorous verbose to show statistics. |
-g | Highlight the last match of the search string. |
-i | Ignore case sensitivity when running the command. |
-m | It adds verbose mode by showing percentages when scrolling. |
-p | Open 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 key | Move the cursor forward one line. |
y or k | Move the cursor backward one line. |
Space key or pg-up | Move forward by a page. |
B or pg-down | Move backward by a page. |
Right arrow | Scroll to the right. |
Left arrow | Scroll to the left. |
Home or g | Move to the start of the file. |
End or G | Move to the end of the file. |
n | Move to the next occurrence of the found string. |
N | Move to the previous occurrence of the search string. |
/string | Search for a string forward. |
?string | Search for a string backward. |
q | Exit 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.
$ less /var/log/kern.log
You will get a screen showing the file’s first page, and you can navigate it using the shortcuts we shared earlier.
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 -N /var/log/kern.log
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.
/DMA
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 -X /var/log/kern.log
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.
$ less -s /var/log/kern.log
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 +F /var/log/kern.log
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.
$ less -pACPI /var/log/kern.log
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.
$ less /var/log/kern.log /var/log/dmesg
At the end of the first page, you will notice that it shows we are currently at (file 1 of 2).
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.
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.’
Example 10: Get a File’s Statistics
You can open a file using the -M option to view statistics about it.
$ less -M /var/log/kern.log
To get more details, press the equals (=) key, and you will notice that you now get more 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:
$ less /var/log/kern.log | grep ‘KVM’
Instead of getting the entire file opened on the screen, only the matching lines are displayed because we added the 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.
$ more /var/log/kern.log
Its output is similar to that of the less command except for the scrolling part.
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:
$ cat /var/log/kern.log
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.