Readers like you help support How-To Geek. When you make a purchase using links on our site, we may earn an affiliate commission. Read More.

Quick Links

Key Takeaways

  • The less command is a more advanced version of the more command, offering more features and functionality for navigating and viewing text files.
  • Less has a wide range of options and keystrokes for navigating, searching, and editing text files, making it a powerful tool for Linux users.
  • It is possible to use less to view and interact with piped input, making it convenient for displaying and navigating through streamed text data.

The less command lets you page through a text file, displaying a screenful of text each time. It seems like one of the simplest Linux commands at first glance, but there's a lot more to less than meets the eye.

The History of less

The less program is based on the more program, which was initially released in 1978 in version 3.0 of the Berkeley Software Distribution Unix (3.0BSD). more allowed you to progressively page through a text file, displaying a screenful of text at a time.

Necessity being the mother of invention, it was the inability of early versions of more to scroll backward through a file that prompted Mark Nudelman to develop less and to overcome that specific problem. That work started in 1983, and the first version was released outside of the company he worked for in 1985. As of October 2019, he is still the maintainer of less.

I wonder if there is a Linux user who hasn't used less? Even if they haven't used it to page through a chosen text file, chances are they've used the man

command. And man calls less behind the scenes to display the man pages.

This is a command with a lot of tricks up its sleeve.

Why less Is Better Than more

less has been added to steadily over the years. It has a staggering amount of command-line options and in-application command keystrokes. Do a quick comparison of the man page for less and the man page for more , and you'll start to see how less absolutely towers over more.

more has overcome its initial failing of not being able to page backward through text, but only for files. It cannot page backward through piped input. You can do that with less.

With its flexibility in navigating files, viewing multiple files, searching for text, dropping and returning to bookmarks, and dealing with piped input, less wins hands down. Use less instead of the more.

Reading a File With less

To load a file into less, provide the name of the file 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 top (or "start") of the file is shown in the terminal window. You can use the scroll wheel of your mouse to scroll forward and backward through the text.

On the keyboard, use the Space bar or Page Down key to move forward through the text one screenful of text at a time.

Page Up will move backward through the file (towards the "start" of the file.) The Home and End keys will take you directly to the start and end of the text file, respectively.

less displaying a text file in a terminal window

The name of the file is displayed in the bottom-left corner of the display. When you start to move around in the file, the bottom line is cleared. It is used to display messages to you, and for you to enter commands in.

Press "q" to quit less.

Displaying Line Numbers

To have the lines of the text file numbered for you, use the -N (line numbers) option.

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

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

The line numbers can be useful to guide you back to specific lines or sections within log files and other files that are not written in standard prose.

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

Searching in less

To search through 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 display. Hit "Enter" to perform the search.

In this example, the search term is "Enfield," and this can be seen at the bottom of the display.

Searching for

The search takes place from the current page to the end of the text file. To search the entire file, move to the top of the file before you search.

You will be told if there are no matches. If a match is found, the display moves to display the found item.

less displaying a matching search item

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

less with two matching search items

To search backward from your current position in the file toward the start of the file, press the "?" key 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're searching backward, the next matching item (found with "n") is the next one nearer to the top of the file, and the "N" for the previous matching item looks for a matching item closer to the bottom of the file. in other words, "n" and "N" reverse their search direction when you search backward.

Opening a File With a Search Term

You can use the -p (pattern) option to cause less to search through the text file and find the first matching item. It will then display the page with the matching search item in it, instead of the first page of the file. Unless, of course, the search item is found on the first page of the file.

Note 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.

less displaying a file with the first matching search item highlighted

Use these keys to move and search through the text file.

  • Move forward one line: Down Arrow, Enter, e, or j
  • Move backward one line: Up Arrow, y, or k
  • Move forward one page: Space bar or Page Down
  • Move backward one page: Page Up or b
  • Scroll to the right: Right Arrow
  • Scroll to the left: Left Arrow
  • Jump to the top of the file: Home or g
  • Jump to the end of the file: End or G
  • Jump to a specific line: Type the line number and then hit "g"
  • Jump to a percentage way through the file: Type the percentage and then hit "p" or "%." (You can even enter decimal values, so to jump to the point 27.2 percent through the file, type "27.2" and then hit "p" or "%." Why would you want to use decimals? I honestly have no idea.)
  • Search forward: Hit "/" and type your search, like "/Jekyll", and press Enter
  • Search backward: Hit "?" and type your search, like "/Hyde", and press Enter
  • Next matching search item: n
  • Previous matching search item: N
  • Quit: q

Squeeze Blank Lines

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

There are a couple of consecutive blank lines in our example file, let's see how less treats them when we use the -s option:

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

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

All of the double (or more) blank lines have been replaced by a single blank line in each case.

Less with no sequences of multiple blank lines being displayed

Viewing Multiple Files

less can open multiple files for you. You can hop back and forth from file to file. less will remember your position in each file.

less Dr-Jekyll-and-Mr-Hyde-001.txt Dr-Jekyll-and-Mr-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 opened, and the first file is displayed. You are shown 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 hit "n".

Your display will change to show the second file, and the information on the bottom line is updated to show you are viewing the second file. This is highlighted below.

viewing the second file in less

To move to the previous file, type ":" and then hit "p."

Using Marks

less lets you drop a marker so that you can easily return to a marked passage. Each marker is represented by a letter. To drop a mark on the top-most displayed line, press "m" and then hit the letter you wish to use, such as "a".

When you press "m", the bottom line of the display shows a prompt as it waits for you to press a letter key.

less prompting for a mark

As soon as you press a letter, the prompt is removed.

From any other location within the file, you can easily return to a mark by pressing the apostrophe (or single quote) "'" and then pressing the letter of the mark you wish to return to. When you press the "'" key, you are prompted for the mark you wish to go to.

less prompting for a mark to return to

Press the letter of the mark you wish to return to, and that section of the text file is displayed for you.

less returning to a mark

Using Piped Input with Less

less can display information that comes as a stream of piped text, just as easily as if it were a file.

The dmesg command displays the kernel ring buffer messages. We can pipe the output from dmesg into less using the following command:

dmesg | less

dmesg | less in a terminal window

The output from dmesg is displayed.

The output from dmesg in less

You can page and search through the piped input just as though it were a file. To see the most recent messages, hit "End" to go to the bottom of the file.

Tme most recent dmesg messages at the bottom of the file in less

As new messages arrive, you must keep pressing "End" to force less to display the bottom of the file. This isn't very convenient. To have less always show the bottom of the text, even when new data is being added, use the +F (forward) option. Note the use of + and not - as the option flag.

dmesg | less +F

dmesg | less +F in a terminal window

The + option flag tells less to treat the option as though you had used that command inside less. So if you forgot to use the +F option, press "F" inside less .

less awaiting new input from dmesg

less displays the bottom of the text, which shows the most recent messages from dmesg. It displays a message that it is waiting for more data. When more kernel messages appear, the display scrolls so that you can always see the newest messages.

You can't scroll or page in this mode; it is devoted to displaying the bottom of the piped text. To exit from of his mode, press Ctrl+c, and you will be returned to the usual less interactive mode.

Editing Files With less

You can edit files with less — well, sort of. This command can't edit files, but if you type "v" when you are viewing a file, the file is transferred to your default editor. When you leave the editor, you are returned to less.

Hit "v" when viewing a file in less:

file displayed in less

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

file loaded in the nano editor

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

In Summary

As counterintuitive as it may seem, in this caseless > more.

Linux Commands

Files

tar · pv · cat · tac · chmod · grep · diff · sed · ar · man · pushd · popd · fsck · testdisk · seq · fd · pandoc · cd · $PATH · awk · join · jq · fold · uniq · journalctl · tail · stat · ls · fstab · echo · less · chgrp · chown · rev · look · strings · type · rename · zip · unzip · mount · umount · install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · vi · nano · mkdir · du · ln · patch · convert · rclone · shred · srm · scp · gzip · chattr · cut · find · umask · wc · tr

Processes

alias · screen · top · nice · renice · progress · strace · systemd · tmux · chsh · history · at · batch · free · which · dmesg · chfn · usermod · ps · chroot · xargs · tty · pinky · lsof · vmstat · timeout · wall · yes · kill · sleep · sudo · su · time · groupadd · usermod · groups · lshw · shutdown · reboot · halt · poweroff · passwd · lscpu · crontab · date · bg · fg · pidof · nohup · pmap

Networking

netstat · ping · traceroute · ip · ss · whois · fail2ban · bmon · dig · finger · nmap · ftp · curl · wget · who · whoami · w · iptables · ssh-keygen · ufw · arping · firewalld