How to Use the less Command on Linux

A Linux terminal on an Ubuntu style desktop.Fatmawati Achmad Zaenuri / Shutterstock

The less command allows you to flick through a text file, displaying a text screen each time. At first glance, this seems to be one of the simplest commands of Linux, but there is much more to do than to see what happens.

The story of less

Everything in Linux – and Unix – has a story, no pun intended. The less program is based on the more program, originally published in 1978 in version 3.0 of Berkeley Software Distribution Unix (3.0BSD). more allowed you to flick through a text file, displaying one text screen at a time.

The need being the mother of the invention, it was the inability of the earlier versions of more to scroll a file back 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's likely that they used the man command. And the man calls less behind the scenes to view the man pages.

It's a command with lots of hints in his sleeve.

Why less is better than more

less has been added to regularly over the years. It has an impressive number of command line options and command keys built into the application. Make a quick comparison of manual page for less and the manual page for more, and you will begin to see how less is fighting absolutely more.

more overcame its initial failure of not being able to go back in the text, but only for the files. It can not go back through a channelized entry. You can do it with less.

With its flexibility in file browsing, viewing multiple files, searching for text, deleting and returning to bookmarks, and processing redistributed input, you'll save less. Use less instead of more.

Read a file with less

To load a file in less, specify the file name on the command line:

less Dr-Jekyll-and-Mr-Hyde-001.txt

less Dr-Jekyll-and-Mr-Hyde-001.txt in a terminal window

The file is loaded and displayed. The beginning (or "start") of the file is displayed in the terminal window. You can use the mouse wheel to scroll the text forward and backward.

On the keyboard, use the space bar or the Page Down key to move forward in the text, screen by screen.

Page Up moves back to the file (to the "start" of the file.) The Home and End keys will take you directly to the beginning and end of the text file, respectively.

less display a text file in a terminal window

The file name is displayed 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 allow you 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.

less -N-Jekyll-and-Mr-Hyde-001.txt

less -N Dr-Jekyll-and-Mr-Hyde-001.txt in a terminal window

Line numbers can be useful for bringing you back to specific lines or sections in log files and other non-standard prose files.

minus displaying a text file with line numbers in a terminal window

Search in less

To search in the text of the file, press "/", 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", which is visible at the bottom of the screen.

Search for "Enfield" in Less

The search takes place from the current page to the end of the text file. To search the entire file, go 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.

minus show a matching search item

To find the next matching article, press "n". To search for the previous matching item, press "N".

less with two matching search items

To search back from your current position in the file up to the beginning of the file, press the "?" Key and type your search term. To find the next matching article, press "n". To search for the previous matching item, press "N".

Note that when you perform a backward search, the next matching item (found with "n") is the next item closest to the top of the file and the "N" of the previous matching item searches for a matching item more near the bottom of the file. in other words, "n" and "N" reverse the direction of the search when you search backwards.

Open a file with a search term

You can use the -p (reason) 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 item is found 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

less -pEnfield Dr-Jekyll-and-Mr-Hyde-001.txt

The file is displayed with the first matching search term highlighted.

display less a file with the first matching search item 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: Down arrow, Enter, e or j
Move backward a line: Up arrow, y or k
Move towards the front a page: Space bar or page at the bottom
Move backward a page: Previous page or b
Scroll to the right: Right arrow
Scroll to the left: Left arrow
Jump to the High the file: Home or g
Jump to the end the file: End or G
Jump to an individual line: Type the line number then type "g"
Jump has a percentage Path in the file: Type the percentage and then press "p" or "%". (You can even enter decimal values.) To move to the 27.2% point in the file, type "27.2" and press "p" or "%." Why would you use decimals? Have no idea.)
Search forward: Press "/" and type your search, like "/ Jekyll", then press Enter
Search back: Press "?" And type your search, like "/ hyde", then press Enter
Next corresponding to research element: not
previous corresponding to research element: NOT
leave: q

Squeeze Blank Lines

The -s (empty lines) option removes a series of blank lines and replaces them with a single blank line.

In our sample file, there are some consecutive empty lines. Let's see how they are treated less when we use the -s option:

less -s-Jekyll-and-Mr-Hyde-001.txt

less -s Dr-Jekyll-and-Mr-Hyde-001.txt in a terminal window

All empty (or more) double lines were replaced by a single blank line in each case.

Less without sequences of several blank lines displayed

Showing multiple files

less can open multiple files for you. You can come and go from file to file. less will remember your position in each file.

less Dr-Jekyll-and-M.-Hyde-001.txt Dr-Jekyll-and-M.-Hyde-002.txt

less Dr-Jekyll-and-Mr-Hyde-001.txt Dr-Jekyll-and-Mr-Hyde-002.txt in a terminal window

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 highlighted below.

less with two files loaded

To view the next file, press ":" and then press "n".

Your display will change to show the second file and the information on the last line will be updated to indicate that you are viewing the second file. This is highlighted below.

view the second file in less

To go to the previous file, type ":" and press "p".

Use of brands

less allows you to drop a marker so you can easily return to a marked passage. Each marker is represented by a letter. To remove a mark on the last line displayed at the top, press "m" and then press the letter you want to use, such as "a".

When you press "m", the last line of the screen displays a prompt until you press a letter.

less incentive to a brand

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 quotation mark) and then the letter of the mark you want to return to. When you press the '' "key, you are prompted to indicate the marker you want to access.

less prompting a brand to return to

Tap the letter of the mark you want to return to and this section of the text file appears.

less return to a brand

Use an entry with less

less can display information as a text stream, as easily as if it were a file.

The dmesg command displays the kernel circular buffer messages. We can direct the output of dmesg in less using the following command:

dmesg | less

dmesg | less in a terminal window

The output of dmesg is displayed.

The output of dmesg less

You can search for a page and search the pipe entry as if it were a file. To see the most recent messages, click on "End" to go to the bottom of the file.

The latest dmesg messages at the bottom of the file in less

As new messages arrive, you must continue to press "End" to force less to display the bottom of the file. That is not very practical. So that less always appears at the bottom of the text, even if new data is added, use the + F (before) option. Note the use of + and non – as an option indicator.

dmesg | less + F

dmesg | minus + F in a terminal window

The option indicator + indicates unless you treat the option as if you had used this command in less. So, if you forgot to use the + F option, press "F" in less.

least waiting for a new dmesg entry

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 you can always see the most recent messages.

You can not scroll or page in this mode; it is dedicated to the display of the bottom of the piped text. To exit its mode, press Ctrl + c and you will return to the usual less interactive mode.

Editing files with less

You can edit files with less than – well, somehow. This command can not edit files, but if you type "v" when viewing a file, the file is transferred to your default editor. When you leave the editor, you return to less.

Press "v" when viewing a file in less:

file displayed less

The file is loaded in the default editor, in this case, nano:

file loaded into the nano editor

When you close the editor, you are turned to less.

In summary

As counter-intuitive as it may seem, in this case> more.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.