
- What is the less command in Linux?
- Basic syntax for the less command in Linux
- Common flags/options for the less command in Linux
- Using the less help option
- Helpful keys to know while using the less command in Linux
- Viewing file contents using the less command in Linux
- Piping other commands into the less command in Linux
- Conclusion
What is the less command in Linux?
Ever heard the expression “less is more”? Well today we’ll be going over the less command in Linux which is very similar to another command called “more”.
Less is a Linux command line tool available on all Linux operating systems and it allows you to page/search through log file output or file contents in pages.
The functionality offered with the less command is extremely useful as you can search forward or backwards for specified strings through multiple files/logs at a time.
Within this article we’ll cover the basics to get you started using the less command and provide quite a few examples of its usage.
Basic syntax for the less command in Linux
The general syntax for using less on the command line interface is as follows:
$ less [flags] example-file.txt
$ less [flags] example-log-file.log
Common flags/options for the less command in Linux
Below is a list of command flags/options which can be used with the less command:
- -E Have the command exit once the end of the output is reached
- -f Using force to open non-regular files such as directories
- -F Have the command exit if the entire file or output can displayed in a single page
- -i Ignore case sensitivity
- -n Remove the line numbers from output in screen
- -p Have the command to start at the first instance of a pattern you’re searching for
- -X Don’t clear the screen/page after quitting the less command
Using the less help option
You can always review the man pages for less or use the following to see all other options available with less on the Linux command line:
$ less --help
Helpful keys to know while using the less command in Linux
Keys | Functionality |
Down Arrows, Enter, e, j | Move one line forward |
Up Arrow, y, k | Move on line backward |
Space Bar, Page Down | Scroll forward one page |
Page up, b | Scroll backward one page |
Left Arrow | Scroll to the left |
Right Arrow | Scroll to the right |
Home, g | Go to beginning of output |
End, G | Go to end of output in file contents |
n | Move to next string match in search (forward search) |
N | Move to previous string match in search (backward search) |
q | Exit |
Viewing file contents using the less command in Linux
Basic viewing of files using the less command in Linux
To view file contents or even log files with less you can simply use the following command syntax in the command prompt:
$ less example-file.txt
When doing so you’ll be able to scroll forward through the entire files contents:

From the above command we’d see output such as in the following screen shot:

And if we hit the “end” key we can skip to the bottom (last line) of the files contents:

All other key shortcuts mentioned in the sections above can be utilized to view entire file contents such as if we wanted to scroll back up using the Up Arrows for moving up one line at a time, or Page Up button to view previous page of output.
Alternatively you can use the Down Arrow to move downward one line at a time, Page Down button to scroll down an entire page, or space bar to scroll forward.
Showing line numbers while viewing files using the less command in Linux
Just as in the previous section we can utilize another one of the flags mentioned to show line numbers within the same file using the following on the command line:
$ less -N example-file.txt

And the command output while using the above less command on this file would look like the following while utilizing -N as a flag:

Searching for strings while viewing files with the less command in Linux
At times it may be needed to look for certain strings through entire files and we can do so using the less command.
To do so, we would enter the file with less and then once we are within the file we would use / followed by the desired search string.
In this example we’re using /define and we’ll see how it highlights lines with that string through the entire file:

After typing in /define we would see the following string become highlighted:

From here we could use n to search forward within the file for each instance of the specified string or N to backward search.
Open a text file with a specified search pattern
Expanding off the previous example we can also open a file with less to find a specified string or search match pattern with the following command in the command prompt:
$ less -pdefine functions.php
The command output would be the same as if we were using /define within the less search while inside the file (like in the previous example) and we’re able to move through the file to each instance of that specified string:

Open multiple files with the less command in Linux
With the less command you’re not just limited to using it on one file at a time and are actually able to run the command on multiple files at a time.
To use less on multiple files you would use the following syntax as example:
$ less functions.php header.php
When opening multiple files with less you move between the two files by entering in the following key which will bring you to the next file to view its contents:
:n
As example in the screen shot below we see how at the bottom of the file (highlighted section) that we have multiple files selected in the less command and we’re currently in file 1 of 2:

Now after entering in ‘:’ followed by ‘n’ we can move from the current file to the next file as shown below:

If you’d like to return back to the previous file you would just need to press ‘:’ followed by ‘p’.
:p
Piping other commands into the less command in Linux
To make your searches even more friendly to the eyes you can pipe other commands into less.
Piping other commands into less (example 1)
For example, lets say we’re browsing through the Apache error log and want to use less but only show output for log entries in reference to a specific IP address.
In such a case we could use the following command syntax in our terminal window to browse through the contents of that log file wherever the IP is referenced:
$ less /var/log/apache2/error_log | grep IP.Address.Goes.Here

This would browse through the Apache error log for instances of where that IP is referenced and with less we can scroll through the output page by page such as we’ve gone over previously.
Piping other commands into less (example 2)
In this example we’re going to be looking into all IP addresses which have been hitting an example website for a specific day and use less to page through the output:
$ grep -s "25/Feb/2025:1" /var/log/apache2/domlogs/* | awk '{print $1}' | cut -d: -f1 | sort | uniq -c | sort -rn | less

From the above we’d obtain command output of grepping for IP requests taking place on Feb. 25th of 2025 through the Apache Domlogs (traffic logs) and we’d print out each IP address to show the number of hits each IP made on that specific date.
By adding less into the command we can scroll through the output page by page and start from the beginning of output.
Conclusion
The less command in Linux offers loads of functionality and is a very popular command line tool which is utilized by Linux Administrators.
With less being offered on all Linux operating systems it is important to learn how to incorporate less either by itself as a command or with other commands on the Linux command line interface.
Through out this article we have covered quite a bit on the less command in Linux to help get you started and provided examples to show-case basic usage of its functionality.
Leave a Reply