Skip to main content

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.

When you open a file with less, various keyboard shortcuts are available to navigate through the file:

KeystrokeDescriptionExample or Additional Details
hHelp screenShows all commands within less
qQuitExits less
SpaceForward one screenScrolls down one screen
bBackward one screenScrolls up one screen
Page DownForward one screenEquivalent to Space
Page UpBackward one screenEquivalent to b
jForward one lineScrolls down by one line
kBackward one lineScrolls up by one line
dForward half screenScrolls down half a screen
uBackward half screenScrolls up half a screen
GGo to endJumps to the end of the file
gGo to startJumps to the start of the file
pGo to a specific percentage50p goes to the middle of the file
: + nGo to specific line number:10 goes to the 10th line
/Search forwards/keyword searches for "keyword"
?Search backwards?keyword searches for "keyword"
nRepeat last search (forward)
NRepeat last search (backward)
FForward forever (like tail -f)
CClear screenRefreshes the screen
m + a-zSet a markma sets a mark named a
' + a-zGo to a mark'a goes to the mark named a
" + a-zGo to a mark on the same line"a goes to mark a on same line
=Show line numbersDisplays the current line numbers
VShow versionShows the version of less
Arrow UpBackward one lineEquivalent to k
Arrow DownForward one lineEquivalent to j
Arrow RightLeft scrollScrolls text to the left
Arrow LeftRight scrollScrolls 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 distinguishes less from more. If you realize that you've missed something important, the b command 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 to g, 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 of less. You don’t have to manually scroll through thousands of lines. Just type / followed by the pattern you are looking for, and less will 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, pressing n will 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, N lets 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

  1. Check System Logs

    less /var/log/syslog
  2. Check Configuration Files

    less /etc/nginx/nginx.conf
  3. Combine with Other Commands

    You can pipe the output of another command to less for easier viewing.

    dmesg | less
  4. View 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.

TypeScript Course Instructor Image
TypeScript Course Instructor Image

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 DETAILS

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

YouTube @cloudaffle