less command in Linux with Examples
Last Updated :
03 Nov, 2025
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:

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:
2. 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:
3. 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. |
|---|
Which command is best for viewing large files page-by-page without opening an editor?
Explanation:
less displays files one page at a time and is optimized for large files.
What happens when you press the Spacebar while using the less command?
-
Goes to the start of the file
-
-
-
Explanation:
Spacebar in less moves the view ahead by one page.
Which less option starts displaying the file from the first match of a given pattern?
Explanation:
The -p option jumps directly to the first occurrence of the pattern.
What does the command dmesg | less do?
-
-
-
Sends dmesg output into less for scrollable viewing
-
Stores dmesg output in a file
Explanation:
The pipe | sends command output to less so it can be viewed page-by-page.
Which option causes less to quit immediately if the file fits on a single screen?
Explanation:
The -F flag exits automatically if the entire content fits on one page.
Quiz Completed Successfully
Your Score : 2/5
Accuracy : 0%
Login to View Explanation
1/5
1/5
< Previous
Next >
Explore
Getting Started with Linux
Installation with Linux
Linux Commands
Linux File System
Linux Kernel
Linux Networking Tools
Linux Process
Linux Firewall
Shell Scripting & Bash Scripting
Linux Administrator System