Open In App

less command in Linux with Examples

Last Updated : 03 Nov, 2025
Comments
Improve
Suggest changes
5 Likes
Like
Report

The less command in Linux is used to view the contents of a file one page at a time without opening it in an editor, making it ideal for reading large files efficiently.

  • It allows you to scroll forward and backward through a file.
  • It does not load the entire file into memory, making it faster for large files.
  • You can search for specific text within the file using /pattern.
  • It provides navigation shortcuts (e.g., Space for next page, b for previous page, q to quit).
  • Commonly used to view log files, configuration files, or command outputs (cat filename | less or less filename)

Using 'less' with Pipelines

The less command can also be used in conjunction with other commands through pipelines. This allows us to view the output of a command directly in the less pager.

Note: I'm using dmesg output as input to less command in the following examples. 

For Example: If you want to read the contents of dmesg command, it's better to use it with fewer command  

dmesg | less

Output:  

348

Syntax:

less [options] filename
  • Here, `filename` represents the name of the file we want to view using the `less` command.
  • The less command provides several options that modify its behavior. Here are some commonly used options:

Examples of `less` command in Linux

Let's look at a few examples to illustrate the usage of the less command with different options.

1. Searching for a pattern

dmesg | less -p "fail"

The above command tells less to start at first occurrence of pattern "fail" in the file and displaying the file from that point.

Output:  

3492. Displaying line number

dmesg | less -N

The -N option displays line numbers along with the file content, allowing you to reference specific lines easily.

Output:

3503. Checking a small file

less -F /home/Mandeep/test/first.erl

In this example, the file `/home//Mandeep/test/first.erl` is small enough to fit on a single screen. The `-F` option causes less to exit immediately without displaying the file since it can be fully shown in one go.

Commonly Used Options in`less`command

Here are the most commonly used and practical options of the less command in Linux:

Options

Description

-E

Automatically exit when reaching the end of the file.

-f

Force non-regular files to be opened.

-F

Exit if the entire file can be displayed on the first screen.

-g

Highlight the string that was found by the last search command.

-G

Suppress highlighting of search matches.

-i

Ignore cases when searching.

-n

Suppress line numbers.

-p pattern

Start at the first occurrence of the specified pattern in the file.

-s

Squeeze consecutive blank lines into a single line.


Explore