The less command is a pager used for displaying large text files (such as big log files) properly. It has better capabilities compared to the more
command. Less command doesn’t read the entire text of a file (unlike the text editors) which results in a faster loading time.
In this article, we will demonstrate the capabilities of less
command with examples.
Also read: The cat and more commands in Linux
Syntax for the less command
The syntax for the less command:
less [options] [filename]
How to read a text file with the less command?
To read a text file with the less command, enter less
followed by the filename
as shown below:
Example :
less Sherlock-Holmes.txt
Output :
Press the Q key on your keyboard to exit from the less command.
How to move across the text file while using less?
Here are the keys frequently used to move across the text file.
KEY | USAGE |
Space or PgDn | Next Page |
b or PgUp | Previous Page |
j or Down Arrow Key or Enter | Next Line |
k or Up Arrow Key | Previous Line |
g or < | First Line |
G or > | Last Line |
nG | Go to the nth Line |
How to display the line numbers in the less command?
The -N option is used with the less
command to display the line number at the beginning of each line in the display.
Example :
less -N Sherlock-Holmes.txt
Output :
If you forget to use the -N option while displaying a text file using less command enter -N from the keyboard to display the line numbers.
How to search in the text file using the less command?
Forward-slash ( / ) followed by the word or pattern (regex) is used to search forward in a text file for the matches in the less command. Similarly, question mark ( ? ) followed by the word or pattern (regex) is used to search backward in a text file.
If there are multiple matches, you can move across the matches by pressing the n key for the next search match and the N key for the previous search match.
By default, the search is case sensitive in the less
command enter -I from the keyboard to ignore the cases.
Example :
Output :
As you can see in the output above, the matches are highlighted. To move to the next/previous match, press n/N keys.
Summary :
KEY | USAGE |
/pattern | Search Forward in the File for the Pattern |
?pattern | Search Backward in the File for the Pattern |
n | Next Search Match |
N | Previous Search Match |
-I | Ignore Case |
How to mark a line using less in Linux?
To mark a line in the less command enter m
followed by a lower or uppercase letter. This will mark the first displayed line with the letter given followed by the m
key.
Example :
ma # Marks the first displayed line with the letter 'a'
Similarly, to mark the last displayed line enter M
followed by a lower or uppercase letter. This will mark the last displayed line with the letter given followed by the M
key.
Example :
Mb # Marks the last displayed line with the letter 'b'
You can see a marked line using the status column. To enable the status column, enter -J
from the keyboard.
Summary :
KEY | USAGE |
m<letter> | Marks the first displayed line with the following letter |
M<letter> | Marks the last displayed line with the following letter |
-J | Enables status column |
less
commandHow to go to a line previously marked with a letter in the less command?
Enter a single quote ( ‘ ) followed by the lowercase or uppercase letter to jump to that line previously marked with that letter.
Example :
'a # Jumps to the line previously marked with the letter "a"
How to monitor a file in real-time using less?
The +F option can be used to follow a file (example, log files) in real-time.
Example :
less +F /var/log/auth.log # Follows auth.log file in real-time
This has similar functionality to the tail -f
command. Read more about the tail command here.
To stop waiting for more data, press CTRL + C
. This will take you back to the normal view of the text file in the less command.
Conclusion
less
command is a great utility to display large text files. less
command has a faster loading time as it doesn’t read the entire text of a file.
In the above article, we have demonstrated the usage of the less
command with examples.
Thank you for reading! 😀
References :