Less Command in Linux:20 Shocking Examples & Shortcuts

less command in linux

less command in linux:Back in the early days of computing, when you opened a huge log file on a terminal, it was as if you were attempting to drink water from a fire hose.

You typed in a command to open the file or “document”, and suddenly thousands of lines of code began rushing past, with you only able to view the end of the file. You had no clue what had come before!

There was a need for a solution to allow sysadmins & devs to limit how much data was being processed at once and to allow them to view the previous & next sections of data to help prevent using too much memory on the machine.

That is why there is the command called "less" command in Linux, which was designed to provide a less resource-intensive alternative to the "more" command, and to provide more functionality than "more".

System administrators and developers needed a way to pause the flowโ€”a way to move backward and forward through data without overloading the computer’s memory. This is where the less command in linux stepped in. It changed the game by being “less” demanding on resources while offering significantly “more” functionality than its predecessor, more.


Table of Contents


What is the less Command?

A terminal paging utility called the less command in linux allows a user to examine the contents of a text file page by page instead of scrolling through an entire file in a text editor (such as vim or nano).

In contrast to traditional text editors, less does not require loading an entire file into memory prior to displaying it, therefore it is extraordinarily fast at displaying large log files that may be multiple gigabytes in size.

The less command is based on the Linux philosophy of “more”, but in a twist of the famous Unix phrase (“less is more, but more than more”) less is a better version of the original "more" command.

Why Use less command in linux Over more or cat?

If you are new to the Linux command line interface (CLI), you might wonder why you can’t just use cat. Here is the breakdown of the hierarchy:

  • cat (Concatenate): Dumps the entire file into the terminal. If the file is 10,000 lines long, youโ€™ll only see the last few lines.
  • more: An older pager that allows you to scroll down. Its biggest limitation is that you cannot easily scroll back up through the text.
  • less: The modern standard. It allows both forward and backward navigation, efficient searching, and it doesn’t wait for the file to finish loading before it starts displaying text.

Basic Syntax and Getting Started

The syntax for the less in linux is straightforward:

less [options] filename

For example, to view a file named server.log, you would type:

less server.log

Once the file is open, your terminal transforms into a viewing window. You won’t see a command prompt at the bottom; instead, you’ll see the filename or a colon (:) indicating the program is ready for a navigation command.

Navigation is where less truly shines. You don’t need to rely solely on arrow keys; these shortcuts make browsing data much faster:

Action Key Shortcut
Scroll Down one line j or Enter
Scroll Up one line k
Scroll Down one window Space or Page Down
Scroll Up one window b or Page Up
Go to the end of the file G
Go to the start of the file g
Quit less q

Searching Within a File using less command in linux

Searching is one of the most powerful features for debugging logs. Use these patterns to find exactly what you need:

  1. Forward Search: Type / followed by your search term and press Enter. (e.g., /error)
  2. Backward Search: Type ? followed by your search term. (e.g., ?warning)
  3. Find Next/Previous:
    • Press n to find the next occurrence in the same direction.
    • Press N to find the previous occurrence.

Advanced Features for Power Users

Multiple File Viewing Options

With the use of less, you can open multiple files simultaneously.

less file1.txt file2.txt

Then, to navigate to the next file, type :n to navigate to the previous file type :p.

File Following

Opening Using Pattern Matching

less +/error system.log

To open a file and jump immediately to the first occurrence of a particular word: less +/error system.log

Some Commonly Used Flag Options

-N
Causes line numbers to appear to the left of each line in the text.
-S
Shortens long lines so that they do not wrap when they reach the right edge of the display. Instead, the text stays on one line, and you can scroll horizontally to view the rest of the text.
-i
Makes searches case insensitive.
-m
Displays the percentage of the file viewed.

20 Frequently Asked Questions (FAQ)

Navigating the Linux filesystem and viewing log files can be daunting for beginners. Using LSI keywords like command line tools, terminal navigation, and file viewing utilities, we’ve answered the most common queries below.

  1. How do I exit the less command in linux?
    Simply press the q key.
  2. Can I edit a file while using command less in linux?
    Yes! Press v to open the file in your default editor (vi/nano).
  3. Does less command in linux work with piped data?
    Yes, e.g., cat file.txt | less.
  4. How do I show line numbers?
    Use less -N filename.
  5. How do I search case-insensitively?
    Use the -i flag or type -i while viewing.
  6. Can less open .gz files?
    Use the zless command for compressed files.
  7. How do I jump to the middle of a file?
    Type 50p to jump to the 50% mark.
  8. What makes less better than more?
    Backward navigation and faster loading speeds.
  9. Does less clear the screen on exit?
    Usually, yes. Use -X to prevent this.
  10. Can I mark a line?
    Press ma to mark, then 'a to return to it.
  11. How do I scroll horizontally?
    Open with -S and use arrow keys.
  12. Is less available on macOS?
    Yes, it is the default pager.
  13. How do I search for a literal “/”?
    Type \/ to escape the character.
  14. Can I change the bottom prompt?
    Yes, using the -P option.
  15. Does it support syntax highlighting?
    Not natively, but you can pipe pygmentize into it.
  16. How do I go to a specific line number using less command in linux?
    Type the number followed by g (e.g., 500g).
  17. What does “End” mean?
    You have reached the bottom of the file.
  18. Can I use it to view script output?
    Yes: ./script.sh | less.
  19. Is less installed on all distros?
    Yes, it is an industry standard.
  20. Why the name “less”?
    A clever pun on the “more” command: “Less is more.”

If you are new to Linux, learning Basic Linux Commands is the first and most important step.
Linux commands help you interact with the operating system using the terminal.
This article from
Coding Journey
explains essential Linux commands in simple language.

Stay Connected with My Coding Journey

Don’t let scammers stop your professional growth. Join our community for more tech safety tips!

For more tutorials and guides, check out: CodingJourney.co.in

Leave a Reply

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