How to search for multiple regex patterns in many files with `ffx`

I recently created a command I named ffx that lets you search your filesystem for files that contain multiple strings or regular expressions. This post describes and demonstrates its capabilities. (There’s a little video down below if you want to see how it works before reading about it.)

Back to top

Background

A few weeks ago I created a “file find” tool that I named “ff”. The best features of ff are:

  • The matching filenames are shown in its output
  • The matching line numbers are shown
  • It highlights your search pattern in the search results (currently underlining it)
  • When I use options like -b 1 -a 1, it shows one line before and one line after each pattern match
Back to top

ffx capabilities

Over the last few days I created a new file-finding tool I named ffx which has these different capabilities:

  • The main feature is that you can search for all files that contain multiple strings or regex patterns. At the moment you can search for up to four matching patterns, but that limit can easily be made higher.
  • As with ff, it shows the matching filenames, line numbers, and you can print lines before or after the matching patterns with the -b and -a options.
  • You can use an “or” option to show all files containing any of the search patterns (instead of showing the files that contain all patterns, which is the default) with the -o option.
  • It supports case-insensitive pattern-matching with the -i option.
  • Like ff, it’s written in Scala and compiled with GraalVM for fast startup times.

Technically it’s possible to combine the two commands, but when I first started developing ffx I didn’t know exactly what I’d end up with, so I created it as a separate command.

One important thing to mention is that ffx assumes that you want to search for at least two patterns. If you just want to search for one pattern you can use ff, the Unix find command, or if you’re using a Mac, use a command like mdfind.

Back to top

ffx usage

Straight from the ffx help text, here are some examples of how to use it:

Search for two patterns:
    ffx -d ~/Flutter -f "*.dart" --p1 ListView --p2 ListTile

The same, but also print one line before and two lines after each match:
    ffx -d ~/Flutter -f "*.dart" --p1 ListView --p2 ListTile -b 1 -a 2

Ignore case when searching:
    ffx -d ~/Flutter -f "*.dart" --p1 ListView --p2 listtile -i

Use the “or” option, searching for files containing either pattern:
    ffx -d ~/Scala -f "*.scala" --p1 ArrayBuffer --p2 ArrayBuilder -o

Search for three patterns:
    ffx -d ~/Scala -f "*.scala" --p1 foo --p2 bar --p3 baz
Back to top

The ffx source code

I created ffx as an open source project, so you can find its source code at this URL:

The README file there provides additional documentation, and shows how to build the command.

Back to top

ffx video/demo

If you like video demos, here’s an example of how ffx works. In this example I search for all *.dart files that contain the strings ListView and ListTile:

If the video stops running, please refresh this page. I created the image as an animated GIF, and set it to repeat ten times (so it will stop after a while).

Back to top

Summary

If you ever wanted a dedicated command that you can use to search many files for multiple strings (or regex patterns), I hope you find this useful.

Alvin Alexander,
Reporting live today from Louisville, Colorado

Back to top