less command in Linux (original) (raw)
Last Updated : 22 Jan, 2026
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
The -N option displays line numbers along with the file content, allowing you to reference specific lines easily.
dmesg | less -N
**Output:
3. Checking a small file
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.
**Command:
less -F /home/Mandeep/test/first.erl
Commonly Used Options in`less`command
Here are the most commonly used and practical options of the less command in Linux:
- **-E: Automatically exits when the end of the file is reached.
- **-f: Forces opening of non-regular files (such as special files).
- **-F: Exits immediately if the entire file fits on the first screen.
- **-g: Highlights only the last searched string.
- **-G: Disables highlighting of all search matches.
- **-i: Ignores case while searching (case-insensitive search).
- **-n: Hides line numbers in the output.
- **-p pattern: Opens the file at the first occurrence of the specified pattern.
- **-s: Combines multiple consecutive blank lines into a single blank line.
