Updated March 28, 2023
Introduction to Less Command in Linux
Less is a utility command in Linux that displays the content of the file or the output of a command. The contents are displayed one page at a time. The less command is particularly useful in viewing large files since it doesn’t load the complete file instead it loads the file part by part. By doing so the access time is much faster.
For example, if you are trying to load a large file using a text editor the entire file is loaded into the main memory which takes more time. The less command loads the file part by part which makes access easier. The less command opens the file in reading the only mode.
Syntax
The general syntax of the less command is given below
less [options] filename
e.g. to display the contents of the file /usr/share/common-licenses/GPL 3 use the following command line.
Code:
$ less /usr/share/ common licenses/GPL 3
Output:
Similarly, we can also view the output of the command by redirecting the output as input to less command as shown below.
$ ps aux | less
To read the contents of the dmesg command pipe it to the less command as shown below
$ dmesg | less
Keyboard Navigation in Less Command
The less program contains a lot of commands that lets you scroll through the contents of the file and search for the desired text. To display the complete list of commands press h. In the table given below, you can see the most commonly used keyboard keys to navigate the file when opened using less command.
Key Command | Action |
Down arrow, enter, e or j | Move down the page by one line |
Up arrow, y or k | Move up the page by one line |
Space bar or f | Move to the next page |
B | Move to the previous page |
Right arrow key | Scroll the page right horizontally |
Left arrow key | Scroll the page left horizontally |
G | Move to the first line |
G | Move to the last line |
5g | Move to the 5th line of the output |
75% or 75p | Move to the line 75% through the output |
n | While searching a particular string move to the next occurrence |
N | While searching move to the previous occurrence |
/pattern, ?pattern | Search a particular string or phrase or pattern in the forward direction and reverse direction respectively |
Q | Quit the less command |
Options Used In Less Command
Let us see some of the commonly used options used with less command.
1. Display Output File With Line Numbers
If you want to display the output with the line numbers use the -N option as shown below
Code:
$ less -N filename
Output:
2. X OPTION
When you exit less command, By default the contents of the file will be wiped off the screen. To keep the contents of the file on screen -X option is used.
Code:
$ less -X filename
3. p OPTION
The –p options tell less to begin at the first occurrence of a particular pattern in the file.
Code:
Dmesg | less –p “failure”
The above-stated command asks less to begin at the first occurrence of pattern failure in the file.
4. Finding Text In Less
If you have opened a large text file and you want to search for a particular text rather than reading it fully just type / followed by the text you want to find. For example, if you want to find the word echo in the file just type /echo. The matched words will be highlighted.
If there is more than one match then you can move to the next match by pressing the ‘n’ key. You can move to the previous match by pressing the ‘N’ key.
The /pattern performs the search in the forward direction and? pattern performs the search in reverse direction.
By default searching using less command is case sensitive. To brush aside the case in less you can use the -I option.
Code:
$ less -I filename
How to Mark Interesting Points In The File?
When you are going through a large text file and at some point, you want to skip a few lines or pages and after some time, you want to come back to that point how will you remember that particular point?. The option is marking.
Less command lets you placemarks or flags to any line of the file. You can simply add the mark by typing the letter ‘m’ followed by a letter.
Code:
mb
if you want to go back to this mark, just press ‘ then press the letter b.
'b
Marking is not limited to a single point. You can mark multiple areas in the file. For each marked position just use a different letter.
Monitoring Files In Real Time Using Less Command
We already know that we can monitor log files using the tail command in real-time. Similarly, we can do the same using less command. When new lines get added to the file we can find that changes in real-time using +F option.
Code:
$ less +F filename
Using the above command will show the final page of a file and it will wait for any new lines or phrase to be added. In this mode, you cannot perform normal scrolling up, down, back or forth actions.
Press Ctrl + C to stop real-time monitoring. The terminal will display the normal view of the command less and then normally exit the file.
Viewing Multiple Files Using Less Command
To open multiple files using less just type the name of the input files one by one as shown below.
Code:
$ less filename1 filename2 filename3
Output:
The files will be listed as per the order mentioned in the command line. To view, other files use the following keys:
- To display the next file press:n
- To display the previous file press :p
Recommended Articles
This is a guide to Less Command in Linux. Here we discuss introduction to Less Command in Linux, used options with codes and outputs. You can also go through our other related articles to learn more –