The less command allows you to browse a text file, displaying a text screen each time. This seems to be one of the simplest Linux commands at first glance, but there is a lot more to it than what seems obvious.
The story of less
Everything under Linux – and Unix – has a story, no pun intended. The less program is based on the more program, which was originally published in 1978 in version 3.0 of the Berkeley Software Distribution Unix (3.0BSD). plus allowed you to progressively browse a text file, displaying one text screen at a time.
The need being the mother of the invention, it's the inability of early versions of more to scroll back into a file that prompted Mark Nudelman to develop less and overcome this specific problem. This work began in 1983, and the first version was published outside the company for which he was working in 1985. In October 2019 he was always the maintainer less.
I wonder if there is a Linux user who has not used less? Even though they did not use it to browse a chosen text file, it is likely that they used the man command. And the man calls less behind the scenes to view the man pages.
It is an order with a lot of tricks in his sleeve.
Why less is better than more
less has been added regularly over the years. It has a staggering amount of command line options and command keys in the application. Make a quick comparison of manual page for less and manual page to find out more, and you will begin to see how less far exceeds absolutely.
more overcame its initial failure to not be able to make a page back in the text, but only for files. It can not go back through a channelized entry. You can do it with less.
Thanks to its flexibility in file browsing, viewing multiple files, searching for text, deleting and returning to bookmarks, and managing entries, Less wins hands down. Use less instead of more.
Read a file with less
To load a file in less, specify the file name on the command line:
The file is loaded and displayed. The top (or "start") of the file is displayed in the terminal window. You can use the scroll wheel on your mouse to scroll forward and backward in the text.
On the keyboard, use the space bar or the Page Down key to move through the text, one text screen at a time.
The previous page will move back to the file (to the "start" of the file.) The Start and End keys will take you directly to the beginning and end of the text file, respectively.
The file name appears in the lower left corner of the screen. When you start moving in the file, the bottom line is erased. It is used to display messages and to enter commands.
Press "q" to quit less.
Display of line numbers
To have the lines of the text file numbered for you, use the -N (line numbers) option.
Line numbers can be helpful in guiding you to specific lines or sections in log files and other files that are not written in standard prose.
Search in less
To search in the text of the file, press "/" and then type your search phrase. The search is case sensitive. Your search phrase is displayed on the bottom line of the screen. Press "Enter" to search.
In this example, the search term is "Enfield", and this can be seen at the bottom of the screen.
The search takes place from the current page to the end of the text file. To search for the entire file, move to the top of the file before starting the search.
You will be told if there is no match. If a match is found, the display moves to display the item found.
To find the next matching item, press "n". To search for the previous matching item, press "N".
To search backward from your current position in the file to the beginning of the file, press the "?" And type your search term. To find the next matching item, press "n". To search for the previous matching item, press "N".
Note that when you search backward, the next matching item (found with "n") is the next closest to the top of the file, and the "N" for the item previous correspondent searches for a matching item closer to the bottom of the file. in other words, "n" and "N" reverse their search direction when you search backwards.
Opening a file with a search term
You can use the -p (template) option so that fewer people look in the text file and find the first matching item. It will then display the page containing the corresponding search item, instead of the first page of the file. Unless, of course, the search item is on the first page of the file.
Note that there is no space between the -p and the search term.
less -pEnfield Dr-Jekyll-and-Mr-Hyde-001.txt
The file is displayed with the first matching search term highlighted.
Navigate in less: the most useful keys
Use these keys to move and search in the text file.
Move towards the front a line: Arrow down, Enter, e or j
Move backward a line: Up arrow, y or k
Move towards the front a page: Space bar or next page
Move backward a page: Previous page or b
Scroll the right: Right arrow
Scroll the left: Left arrow
Jump the top the file: Home or g
Jump the end the file: End or G
Jump to a specific line: Enter the line number and press "g"
Jump has a percentage Path through the file: Type the percentage, then press "p" or "%". (You can even enter decimal values, so to reach the 27.2 percent point in the file, type "27.2" then press "p" or "%." Why would you use decimals? Honestly, I have no idea.)
Search forward: Press "/" and type your search, like "/ Jekyll", then press Enter
Search back: Press on "?" And type in your search, like "/ Hyde", then press Enter
Next corresponding to research element: not
previous corresponding to research element: NOT
Compression of blank lines
The -s (empty line compression) option removes a series of empty lines and replaces them with a single blank line.
There are a few blank lines in our sample file, let's see how less is processed when we use the -s option:
All double (or more) empty lines have been replaced by a single empty line in each case.
Showing multiple files
less can open multiple files for you. You can come and go from one file to the other. less will remember your position in each file.
less Dr-Jekyll-and-Mr-Hyde-001.txt Dr-Jekyll-and-Mr-Hyde-002.txt
The files are open and the first file is displayed. You see which file you are looking at and how many files have been loaded. This is underlined below.
To view the next file, press ":" and then press "n".
Your display will change to show the second file and the information on the bottom line will be updated to show you are viewing the second file. This is underlined below.
To go to the previous file, type ":" and then press "p."
Use of brands
Less lets you drop a marker so you can easily return to a marked passage. Each marker is represented by a letter. To drop a mark on the most displayed line, press "m" and then press the letter you want to use, such as "a".
When you press "m", the bottom line of the screen displays a prompt while it waits for you to press an alphabetic key.
As soon as you press a letter, the prompt is deleted.
From any other location in the file, you can easily return to a mark by pressing the single quotation mark (or single quotation mark), then the letter of the mark you want to return to. When you press the '' "key, you are prompted to indicate the mark you want to go to.
Tap the letter of the mark you want to return to and this section of the text file is displayed for you.
Using channelized input with less
less can display information as a stream of channelized text, as easily as it was a file.
The dmesg command displays the kernel ring buffer messages. We can direct the output of dmesg to less by using the following command:
dmesg | less
The output of dmesg is displayed.
You can page and search in the channelized entry as if it was a file. To see the most recent messages, press "End" to go to the bottom of the file.
As new messages arrive, you must continue to press "End" to force less to display the bottom of the file. This is not very practical. So that less always displays the bottom of the text, even when new data is added, use the + F (before) option. Note the use of + and non – as an option indicator.
dmesg | less + F
The option indicator + tells less to treat the option as if you had used that command inside less. So, if you forgot to use the + F option, press less on "F" on the inside.
less displays the bottom of the text, which displays the most recent messages from dmesg. It displays a message stating that it is waiting for more data. When more kernel messages appear, the display scrolls so that you can always see the most recent messages.
You can not scroll or flick through this mode; it is dedicated to the display of the bottom of the channeled text. To exit its mode, press Ctrl + c, and you will return to the usual least interactive mode.
Edit files with less
You can edit files with less – well, sort of. This command can not edit files, but if you type "v" when you view a file, the file is transferred to your default editor. When you leave the editor, you return to less.
Press "v" when you view one less file:
The file is loaded in the default editor, in this case nano:
When you close the editor, you are turned to less.
As counter-intuitive as it may seem, in this case> more.