Understanding the `less` Command in Linux
Linux has a rich set of command-line tools for text manipulation, and one of the
most commonly used commands for viewing text files is less. The less command
is a terminal pager program that allows you to view large files in a way that is
faster, more efficient, and more feature-rich compared to other text viewing
commands like cat and more. This article delves into the details of
the less command and explores its functionalities, options, and use-cases.
Basic Usage
The simplest way to use less is to open a file for viewing:
less filename.txt
This will display the content of filename.txt one screen at a time. You can
navigate through the file using your keyboard. For example, pressing the space
key scrolls down, while b scrolls back up. Press q to quit and return to the
command line.
Navigation Commands
When you open a file with less, various keyboard shortcuts are available to
navigate through the file:
| Keystroke | Description | Example or Additional Details |
|---|---|---|
h | Help screen | Shows all commands within less |
q | Quit | Exits less |
Space | Forward one screen | Scrolls down one screen |
b | Backward one screen | Scrolls up one screen |
Page Down | Forward one screen | Equivalent to Space |
Page Up | Backward one screen | Equivalent to b |
j | Forward one line | Scrolls down by one line |
k | Backward one line | Scrolls up by one line |
d | Forward half screen | Scrolls down half a screen |
u | Backward half screen | Scrolls up half a screen |
G | Go to end | Jumps to the end of the file |
g | Go to start | Jumps to the start of the file |
p | Go to a specific percentage | 50p goes to the middle of the file |
: + n | Go to specific line number | :10 goes to the 10th line |
/ | Search forwards | /keyword searches for "keyword" |
? | Search backwards | ?keyword searches for "keyword" |
n | Repeat last search (forward) | |
N | Repeat last search (backward) | |
F | Forward forever (like tail -f) | |
C | Clear screen | Refreshes the screen |
m + a-z | Set a mark | ma sets a mark named a |
' + a-z | Go to a mark | 'a goes to the mark named a |
" + a-z | Go to a mark on the same line | "a goes to mark a on same line |
= | Show line numbers | Displays the current line numbers |
V | Show version | Shows the version of less |
Arrow Up | Backward one line | Equivalent to k |
Arrow Down | Forward one line | Equivalent to j |
Arrow Right | Left scroll | Scrolls text to the left |
Arrow Left | Right scroll | Scrolls text to the right |
This table should now give a comprehensive overview of the keyboard navigation possibilities within the less command.
More about the Commands
space: When you're reading a large file, scrolling down one screenful at a time lets you read through the content in a controlled manner. This prevents you from having to manually scroll, making it easier to maintain your reading focus.b: The ability to scroll back is what distinguisheslessfrommore. If you realize that you've missed something important, thebcommand is invaluable for going back and reviewing content.g: Jumping to the beginning of a file is extremely useful when you're deep within a large document and you quickly want to go back to the start, without having to repeatedly press keys to scroll back.G: The counterpart tog, this command lets you quickly jump to the end of a file. It's particularly useful when you are searching for information that you know is near the end of a file./pattern: Searching is one of the key strengths ofless. You don’t have to manually scroll through thousands of lines. Just type/followed by the pattern you are looking for, andlesswill navigate to the next instance of this pattern.?pattern: Sometimes, you realize that the information you are looking for was actually before the current position in the document. Use?followed by the pattern to search backwards through the document.n: Once you've initiated a search, pressingnwill continue that search and take you to the next occurrence of the pattern. This is a powerful way to navigate through search results without having to retype the search term.N: If you've been searching in one direction and want to reverse your direction,Nlets you do that easily. This can be particularly useful when you've reached the end of the document and want to check for other occurrences in the opposite direction.
Understanding how to navigate with less effectively can make your text file
exploration much more efficient. The command offers a range of features to help
you quickly find and view the information you need.
Key Features
Searching
As noted above, you can search within the file. This can be especially useful
when viewing log files or code. Simply use / followed by the pattern to search
forward and ? to search backward.
Line Numbers
You can view line numbers by typing -N while you're inside the less viewer.
This is especially useful if you're looking at a code file and want to reference
line numbers.
Viewing Multiple Files
You can open multiple files by passing their names:
less file1.txt file2.txt
Navigate between files using :n for the next file and :p for the previous
file.
Other Options
less has a plethora of options to tailor its behavior:
-S: Chop long lines instead of wrapping them.-m: Display more verbose prompt.-E: Automatically exit if the end of the file is reached.
Practical Examples
Check System Logs
less /var/log/syslogCheck Configuration Files
less /etc/nginx/nginx.confCombine with Other Commands
You can pipe the output of another command to
lessfor easier viewing.dmesg | lessView Compressed Files
You can also view compressed files directly:
less myfile.gz
Fun Fact: less is more
The less command was actually derived from the more command in Unix.
The more command also allows you to view text files one screen at a time.
However, less offers more features and is generally more versatile.
The primary difference lies in navigation. In more, once you scroll down, you
can't scroll back up again. This is where less improves upon more — it
allows for both forward and backward navigation. The name less is a bit of a
joke, suggesting that 'less is more' because it provides more functionality
than more while offering a similar user experience.
In summary, less is an incredibly useful command in the Linux toolkit, giving
users a powerful way to navigate and manipulate text right from the command
line. It is an indispensable tool for anyone who works in a Linux environment.


Time To Transition From JavaScript To TypeScript
Level Up Your TypeScript And Object Oriented Programming Skills. The only complete TypeScript course on the marketplace you building TypeScript apps like a PRO.
SEE COURSE DETAILSWhat Can You Do Next 🙏😊
If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for ssoftware developers.