Shell Tricks: instant wildcard directory search

Here’s a handy trick I use at the command line to quickly find filenames matching simple wildcard patterns in nested folders. It’s called lsgrep because that’s essentially what it does: grep a list of filenames and return matches.

Dependencies

This function uses The Silver Searcher (ag), which can be installed via Homebrew (brew install the_silver_searcher). It could easily be modified to work with ack or other grep replacement (or grep itself, with the right options).

Installation

Copy the function below into your ~/.bash_profile (or wherever you source functions from at login). Source the file to test it out (e.g. source ~/.bash_profile).

By default it recurses 3 levels deep into folders. You can modify this in the last line by changing the number after the --depth flag.

Wildcards

You’ll note that when it creates the $NEEDLE search term, it substitutes basic “*” and “?” wildcards for actual regular expressions. It also escapes “.” to search for a literal period. Spaces are converted to “.*” searches, meaning any number of characters can exist between the arguments. Thus:

In the first example you can see that the “?” wildcard only allowed one random character between “d” and “rty”, so the file containing “dirty” was returned. In the second example the search was converted to “d.*rty”, so any file with containing a “d” and then an “rty” at any point after is matched.

Limit By Filetype

The literal period substitution allows you to add an extension at the end of the query to limit the filetype.

It’s a great shortcut to find files you know the name of, and in iTerm you can then just Command-Click a result to open it in the appropriate editor. You can also use it in another command like cat $(lsgrep d?rty). I hope you find it as useful as I do!