less command in Linux with Examples
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 | lessOutput:
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 -NThe -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.erlIn 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?
-
A
cat
-
B
nano
-
C
less
-
D
tail
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?
-
A
Goes to the start of the file
-
B
Scrolls one page forward
-
C
Quits the viewer
-
D
Searches for a pattern
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?
-
A
less -i pattern
-
B
less -p pattern
-
C
less -s pattern
-
D
less -g pattern
The -p option jumps directly to the first occurrence of the pattern.
What does the command dmesg | less do?
-
A
Filters dmesg logs
-
B
Opens dmesg in an editor
-
C
Sends dmesg output into less for scrollable viewing
-
D
Stores dmesg output in a file
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?
-
A
-E
-
B
-F
-
C
-n
-
D
-s
The -F flag exits automatically if the entire content fits on one page.
