Sep 7, 2022 3 min read

How to Search in Less Command

Table of Contents

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.

Searching in less command

It will start a forward search from your current location and move you to the first found match. The matched patterns are highlighted.

Serached patterns are highlighted in the less command
Notice the highlighted matched pattern
  • You can move to the next matched pattern by pressing the n key.
  • You can move back to the previous match by pressing the N (shift+n) key.

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.

Searched pattern not found with less command
💡
You can perform a backward search with ?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:

-I

You can use it before you start a search or during a search. It works the same.

Run case insensitive search
Use -I to toggle the case-sensitive search

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 however, 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
💡
You can perform an inverted search with /!pattern and search for lines that do not match the pattern.

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 /pattern.

&pattern

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 -pI.

What else?

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.

Abhishek Prakash
Creator of Linux Handbook and It's FOSS. An ardent Linux user & open source promoter. Huge fan of classic detective mysteries from Agatha Christie and Sherlock Holmes to Columbo & Ellery Queen.
Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Linux Handbook.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.