Methods of payment Abuse

Search for files by content in Linux: ack and ripgrep

03.05.2023, 23:33

Using file search by content in Linux allows you to significantly reduce the time and effort when searching for specific information in large and complex file systems.

Why do I need to search for files by content

This search can be especially useful when you need to find files containing a specific string or keyword, as well as in cases where you need to find all files that match a certain template or criterion.

File search by content can be used for various tasks, for example, to search:

  • specific text in large text files;
  • configuration files and scripts containing certain settings or commands;
  • of files containing certain data, such as user names or phone numbers;
  • of files by content type, for example, searching for all files containing JPEG images or PDF documents.

The general purpose of searching files by content is to facilitate working with the file system and finding the necessary information in the file system.

ack

Let's start with the ack utility. It was created in 2005 as an alternative to grep and quickly became popular among developers.

It has several advantages over grep, including:

  • more intelligent search by files and exceptions. By default, Ack searches only in files that may make sense, for example, excluding .svn or .git. And you can add your own rules and exceptions;
  • search in hidden files and directories by default$
  • Ack doesn't try to search everywhere, even in system folders like grep. This means that you can easily find files hidden in the system.

Examples of using ack:

  1. Find all files in the current folder and its subfolders containing the word "test": ack "test"
  2. The same, but excluding files with the extension .bak: ack "test" --ignore-file=match:/\.bak$/
  3. Find all files in the current folder containing the string "hello" and output the first 2 lines of each file: ack "hello" --heading --max-count=2
    ripgrep

The second interesting tool is ripgrep. It is similar to ack in many ways, but uses a faster search algorithm, which makes it especially useful for large projects.

Here are some examples:

  1. Find all files in the current folder and its subfolders containing the word "test": rg "test"
  2. The same, but excluding files with the extension .bak: rg "test" --glob "!*.bak"
  3. Find all files in the current folder containing the string "hello" and output the first 2 lines of each file: rg "hello" --heading --max-columns=2

At the end, it is worth mentioning fzf. It's not just a string search engine like ack and ripgrep. Fzf is used as a utility to search for files, folders, and items in a list. Otherwise, it works similar to ack/ripgrep, but with the added possibility of live filtering of results. Here are some examples:

  1. Search for files starting with the letter F on your computer: locate F | fzf -m
  2. Exit the current folder and go to the directory I want: cd **<TAB> |fzf
  3. Select SVN revisions and copy the number to the clipboard: svn log | fzf --reverse --ansi | awk '{print $1}' | pbcopy

We hope that these tools will help you find the files and strings you need quickly and efficiently. Whichever one you choose, they all offer advanced search that can significantly speed up your work.