How to Use Less Command for Searching Patterns [5 Best Ways]

Written by

Reviewed by

Last updated: July 24, 2024

Expert verified

SVG Image

TL;DR

To use the less command for searching patterns in Linux, you can try these methods:

  1. Search for a specific pattern in a text file with less interface and then execute /search-pattern.
  2. Reverse search for a pattern starting from the end of the file by running ?search-pattern in the less interface.
  3. Conduct a case-insensitive search with less -I filename followed by /search-pattern.
  4. Display only the lines containing a matching pattern by using the &/pattern command.
  5. Search for a pattern from the start of a file with the less -p search-pattern filename command.

Learn more about using the less command for searching patterns, avoiding common mistakes, and knowing the benefits of using this command in Linux.

Struggling to find information in large text files? The Less command in Linux can help. In this post, I’ll guide you through using the Less command to search for patterns and fix common errors. You’ll learn easy search techniques, like finding specific patterns, reverse searches, and case-insensitive searches. I’ll also show you how to display only matching lines, start searches from the beginning, and avoid mistakes. By the end, you’ll be able to search text files like a pro.

What is the Less Command?

The Less command is a tool in Linux that lets you view and navigate the contents of text files. Unlike other commands, such as cat or more, Less allows you to scroll through files both forwards and backward. This makes it very useful when dealing with large files. You can open a file with Less by typing less filename in the terminal. Once the file is open, you can move around easily using keyboard shortcuts.

Advantages of Using Less

  • Bidirectional Scrolling: You can scroll up and down through the file, which is helpful for reading large files.
  • Efficient for Large Files: Less doesn’t load the entire file into memory, making it faster and more efficient for viewing large files compared to cat.
  • Search Functionality: You can search for specific patterns within the file using the forward slash (/) followed by the search term. This makes it easy to find specific information quickly.
  • Navigation Shortcuts: Less provides handy keyboard shortcuts for quick navigation. For example, you can use the space bar to move forward one screen or the b key to move back one screen.
  • Piping Capability: You can use Less to view the output of other commands by piping, like dmesg | less. This allows you to scroll through and search command outputs easily.
  • Customization: You can customize Less using configuration files or command-line options to suit your preferences, such as displaying line numbers or enabling horizontal scrolling.

How to Install Less in Different Linux Distributions

Installing Less on your Linux system is straightforward. Here’s how you can do it:

  • Checking if Less is Installed:

Before installing, you should check if Less is already on your system. Open your terminal and type:

less --version

If you see the version number, Less is already installed. If you get a “command not found” message, you need to install it.

  • Installing Less on Debian/Ubuntu

To install Less on Debian or Ubuntu, use the apt-get command:

sudo apt-get install less

This command downloads and installs Less.

  • Installing Less on Red Hat/CentOS

For Red Hat or CentOS systems, use the yum command:

sudo yum install less

This command fetches and installs Less.

  • Installing Less on Arch Linux

On Arch Linux, you can install Less with the pacman command:

sudo pacman -S less

This command installs Less on your system.

How to Use less Command for Searching Patterns

To use the Less command for searching patterns, open the file with less filename, then type / followed by the pattern you want to search and press Enter. For reverse searches, use ? followed by the pattern. Use n to move to the next match and N for the previous match. To make searches case-insensitive, use less -I filename.

Here’s the breakdown of each method:

1. Searching for a Specific Pattern

You can use the less command for searching a particular pattern within a text file. It’s the simplest way to easily locate specific search terms, saving you a significant amount of time and effort. Here’s the step-by-step guide for this method:

  1. Execute the following command to open the file you want to search using the less command.
less filename
  1. Type / followed by the pattern you want to search for. For example, to search for the word “Linux“, type the command below and press Enter.
/Linux
  1. You’ll see the matching pattern highlighted on the Terminal screen. To navigate through the search results, you can press n for the next and N for the previous occurrences.
matching pattern will be highlighted on the terminal screen less command for searching

2. Reverse Pattern Searching

With this method, you can use the less command to search for a pattern starting from the end of each page. In some less versions, it might jump to the next page. This is helpful when you’re looking for a term closer to the end. Follow the steps below to use this method:

  1. Open the file you want to search using the less command in the Terminal window.
less filename
  1. Type ? followed by the pattern you want to search for. For example, to search for the word “Linux” from the end of the file, type the following command and press Enter.
?Linux
  1. After finding the first occurrence of the pattern, it will be highlighted on the screen. You can navigate to the next and previous matches by pressing n and N.
after finding the first occurrence of the pattern

When searching for patterns with the less command, the default mode is case-sensitive. But there might be situations where you want to search for a pattern regardless of its case. In such cases, you can turn on the case-insensitive mode by using the -I option with the less command. Here’s how to do it:

  1. In the Terminal window, type the command below and press Enter.
less -I filename
  1. The command will open the file and let you search for a pattern without worrying about its letter case.
search for a pattern without worrying about its letter case
  1. To search for a pattern, type / followed by the pattern you want to search for and press Enter.
/LINUX
  1. The matching pattern will be highlighted on your Terminal screen. To find the next occurrence, press the n key. To find the previous occurrence, press the N key.
matching pattern will be highlighted on your terminal screen

4. Display Matching Lines Only

This command will search for the pattern in the file and display only the matching lines without highlighting the pattern. his can be useful when you want to quickly view the lines that contain a particular pattern without having to navigate through the entire file. To use this method, follow these steps:

  1. Run the following command to open the less view of the file content in the Terminal window:
less filename
  1. To search for a pattern, type & followed by the pattern you want to search for and press Enter.
&/Linux
  1. The less viewer will display only the lines that contain the pattern, without highlighting it. You can use the arrow keys or space bar to navigate through the matching lines search results.
less viewer will display only the lines

5. Searching from the Start of a File

This command will open the file in the less viewer with the search results for the pattern entered wi the less command in the Terminal window. Let me explain this method with the steps below:

  1. In the Terminal window, type the command below and press Enter.
less -p search-pattern filename
  1. The less viewer will open and display the contents of the file. The first occurrence of the pattern will be highlighted on the screen.
less viewer will open and display the contents of the file
  1. To find the next occurrence, press the n key. To find the previous occurrence, press the N key. Use the arrow keys or the space bar to navigate through the search results of the file in the less interface.
use the arrow keys or the space bar to navigate

4 Common Mistakes When Using the less Command

While the less command for searching patterns is a great option for low-end Linux systems and simple search operations, there are some common mistakes that you can make. Here are four of them:

  • Using the Wrong Syntax: The less command requires specific syntax to work correctly. Make sure you understand the syntax before using a command to avoid errors. For example, use less +/pattern file to search for a pattern in a file.
  • 💾 Forgetting to Save Search Results: If you find a useful search result, don’t forget to save it for future reference. You can save search results using the | command followed by a filename. For example, to save search results to a file named “results.txt“, type | tee results.txt and press Enter while in less interface.
  • 🧠 Not Taking Advantage of Advanced Search Techniques: While the basic search techniques are useful, advanced search techniques like regex can help you find patterns more quickly and efficiently. Use the command less -R file for regular expression searching.
  • 🔧 Failing to Filter Search Results Effectively: If you’re searching through a large file, filtering search results can help you find the information you need more quickly. Make sure you understand how to filter search results effectively. For instance, you can use grep in combination with less like this: grep 'pattern' file | less.

Wrapping Up

In this article, I’ve shown you how to use the Less command to search for patterns in text files. I’ve covered methods like searching for specific patterns, reverse pattern searching, and case-insensitive searches. I also explored some common mistakes and troubleshooting tips.

To expand your Linux knowledge, check out articles on other powerful text processing tools:

  • Discover advanced Grep techniques, like using the OR condition, to combine multiple search patterns and refine your search results.
  • Learn how to use the Find command to efficiently locate files and directories across your system, helping you keep your filesystem organized.
  • Explore how to count lines in a file, which can give you quick insights into the size and structure of your text files, making data management easier.

Frequently Asked Question

How do I search for a pattern case insensitively?

To search for a pattern case insensitively, use the -i (ignore-case) option. This option tells less to ignore the case of the pattern you are searching for. For example, to search for the word “Linux” case-insensitively, type less -i filename and press Enter. Note that the -i option only applies to the search pattern and not to the filename or other options.

Can I search for patterns across multiple files at once?

Yes, you can use the * (wildcard) to search for patterns across multiple files at once. This allows you to search for a pattern in all files that match a certain pattern in their name. For example, to search for the word “Linux” in all text files in the current directory, type less *txt /Linux, and press Enter. You can also use regular expressions to match file names.

How can I change the display of search results?

To change the display of search results, use the --color option followed by a color option. This option allows you to specify the color in which search results are displayed. For example, to display search results in red, type less --color=red filename and press Enter. You can also use other options to control how search results are displayed, such as the number of lines before and after the search result that are displayed.

Can I use less to search for patterns in binary files?

No, less is designed to view and search through text files only. It may not work properly or produce unexpected results if used to search through binary files. For searching through binary files, you can use other tools like grep or specialized binary file editors.

How can I filter search results by specific characters?

To filter search results by specific characters using less, open the file with less filename and press Enter. Then, type / followed by the pattern you want to search for and & followed by the specific character(s) you want to filter by. For example, to display only lines that contain the word “Linux” and end with a period, type /Linux&\./ and press Enter.

How can I filter search results by lines?

To filter search results by specific line numbers using less, open the file with less filename and press Enter. Then, type : followed by the line number you want to filter. For example, to display only line 10, type :10 and press Enter.

Ojash

Author

Ojash is a skilled Linux expert and tech writer with over a decade of experience. He has extensive knowledge of Linux's file system, command-line interface, and software installations. Ojash is also an expert in shell scripting and automation, with experience in Bash, Python, and Perl. He has published numerous articles on Linux in various online publications, making him a valuable resource for both seasoned Linux users and beginners. Ojash is also an active member of the Linux community and participates in Linux forums.

Akshat

Reviewer

Akshat is a software engineer, product designer and the co-founder of Scrutify. He's an experienced Linux professional and the senior editor of this blog. He is also an open-source contributor to many projects on Github and has written several technical guides on Linux. Apart from that, he’s also actively sharing his ideas and tutorials on Medium and Attirer. As the editor of this blog, Akshat brings his wealth of knowledge and experience to provide readers with valuable insights and advice on a wide range of Linux-related topics.

Share this article
Shareable URL
Prev Post

How to Delete Lines in a File Using Vim [8 Easy Ways]

Next Post

How to Make a File Executable in Linux [3 Best Ways]

Read next