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
Everything in Linux–and Unix—has a history, no pun intended. 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
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
less behind the scenes to display the
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 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
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
Reading a File With less
To load a file into
less, provide the name of the file on the command line:
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.
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
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
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.
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.
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.
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 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
The file is displayed with the first matching search term highlighted.
Navigating in Less: The Most Useful Keys
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
-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
less -s Dr-Jekyll-and-Mr-Hyde-001.txt
All of the double (or more) blank lines have been replaced by a single blank line in each case.
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
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.
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.
To move to the previous file, type “:” and then hit “p.”
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.
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.
Press the letter of the mark you wish to return to, and that section of the text file is displayed for you.
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.
dmesg command displays the kernel ring buffer messages. We can pipe the output from
less using the following command:
dmesg | less
The output from
dmesg is displayed.
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.
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
+ 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 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
Hit “v” when viewing a file in
The file is loaded into the default editor, in this case
When you close the editor, you are turned to
As counterintuitive as it may seem, in this case
|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|
|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|
|Networking||netstat · ping · traceroute · ip · ss · whois · fail2ban · bmon · dig · finger · nmap · ftp · curl · wget · who · whoami · w · iptables · ssh-keygen · ufw|
- › How to Use the wc Command in Linux
- › How to Use the timedatectl Command on Linux
- › Easily Understand Your Linux RAM Usage With Smem
- › How to Read Data From a Socket in Bash on Linux
- › How to Review sudo Command Usage on Linux
- › How to Get Started With firewalld on Linux
- › How to List the Installed Packages on Linux
- › How to Insert Data From a Picture in Excel on Windows