Methods of payment Abuse

Searching files by content in Linux: ack and ripgrep

03.05.2023, 23:33

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

Why you need to search for files by content

This search can be particularly useful when you want to find files that contain a certain string or keyword, or when you want to find all files that match a certain pattern or criterion.

Searching for files by content can be used for a variety of tasks, such as finding:

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

The general purpose of searching files by content is to make it easier to work with the file system and find the information you need 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:

  • smarter file and exception searches. Ack by default only searches in files that might make sense, such as excluding .svn or .git folders. And you can add your own rules and exceptions;
  • Ack searches hidden files and directories by default;
  • Ack doesn't try to search everywhere, even system folders, like grep. This means you can easily find files hidden on your system.

Examples of how to use ack:

  • Find all files in the current folder and its subfolders that contain the word "test": ack "test"
  • Same thing, but excluding files with the .bak extension : ack "test" --ignore-file=match:/.bak$/
  • 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 that contain the word "test": rg "test"
  2. Same thing, but excluding files with a .bak extension : 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's worth mentioning fzf. It's not just a string finder like ack and ripgrep. Fzf is used as a utility to find files, folders and items in a list. Otherwise, it works similarly to ack/ripgrep, but with the added ability to live filter the results. Here are some examples:

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

Hopefully these tools will help you find the files and strings you need quickly and efficiently. Whichever you choose, they all offer advanced searches that can greatly speed up your work.