How to Search in Less Command
The less command is excellent for reading large text files. It also allows you to search for text in it. Here's what you need to know about searching in less.
The less command is excellent for viewing the contents of a text file in the terminal without cluttering your screen.
If you are viewing a large file and looking for a particular text in it, you don't need to do it manually.
You can perform search in the less command. Let me show you how.
Search with less command
Open the file to view with the less command.
And then press the / key followed by the pattern you want to search for and press the enter key.
It will start a forward search from your current location and move you to the first found match. The matched patterns are highlighted.
- You can move to the next matched pattern by pressing the
- You can move back to the previous match by pressing the
You may also move up and down the page using the space and b keys. The matched patterns, if any, are highlighted.
If the search pattern is not found, you should see a "Pattern not found (press RETURN)" message at the bottom.
?pattern instead of
/pattern. It will start searching backward from your current location.
Perform a case insensitive search with less
By default, the search in less is case-sensitive.
To run a case-insensitive search, you can enter the following in less view:
You can use it before you start a search or during a search. It works the same.
Actually, it toggles the case-sensitive search. You press it again, and the search will be case-sensitive again.
You may also use
i instead of
i won't perform a case-insensitive search if there is an uppercase letter in the pattern you are searching. This is why
I is the better option here.
If you want, you can start less with case insensitive mode from the beginning:
less -I filename
Show only matching lines
Don't feel like pressing n or N to see the matching patterns? You can only show the matching lines in less by performing the search with
&pattern instead of
Unlike /pattern, you don't see the matching pattern highlighted anymore. You just get a bunch of lines that have the searched pattern in them.
You can use the arrow key to move to the lines. If you look at the bottom, you'll notice that it shows the line numbers and they are not consecutive as you are only seeing the matching lines.
Start a search along with less command
Yes. You can start searching for a term as soon as you open the file with the less command.
less -p pattern filename
You can perform a case insensitive search by adding the option
-I like this:
less -Ip pattern filename
Do note that the order of the options matter here. The above command won't work if you use
Since less is just a viewer, you can not do a find-replace like Vim here.
There are some other search features in less when you are viewing the content. You can read its man page for more details.
In my opinion, it is okay to search with less when you are viewing a file. However, for serious searches inside file text, you should rely on the grep command.