i CLI search on macOS · Dark Matter Industries

CLI search on macOS

Since the dawn of time, well, of my time on Unix, I have always used the venerable find command to search for files or directories on my machines.

The muscle memory is so built-in that even though I know of better tools, I keep reaching for it.

Of course, one of the most powerful features of find is the ability to then manipulate what you have found within the find interface itself. Sure you can pipe things forward with any tool that puts the path of the file out in stdout but it’s a nice Swiss Army Knife.

In addition, various options allow you to really fine tune your search. Looking for a file modified at a certain time? Use the flag -mtime. Want to use regex instead of globbing? Use the flag -iregex. You can even search for inode numbers, if you are lost in a tangle of hard-links, with -inum.

But the one disadvantage of find is that it does not use a search index. This means that find is really a bg kind of tool, especially if you are performing deep searches.

The time to return the results is acceptable if you are doing meta-data search and you want to make use of the various post-search capabilities of find. But what if you just want to find a file from its filename and it’s lost somewhere in the logical system of directories that you created in 1992.

Well, if you have a Mac, you are in luck. Since Mac OS X Tiger in 2005, the Apple computer has had indexed search capability. Essentially a daemon called mds (or mdworker; md = (m)meta(d)ata) monitors changes to the filesystem and indexes files and their metadata.

Apple, being Apple, built an easy-to-use interface called Spotlight for the masses. Since a Mac is really a variant of Unix called Darwin, what do you want to bet that there is a command line command that allows you to access that stored search index?

It’s called mdfind and is very simple with a few options. However, if you simply want to find a file based on its name.

 % mdfind -name my_missing_file

and, like Spotlight, it is fast. If you find without the -name flag it will search in metadata as well.

This being an Apple product, you can search directly in metadata such as kMDItemUserTags to find items that are tagged in the finder, kMDItemAperture for images that have EXIF data with the aperture size that you want. Et cetera.

And the metadata does not stop with Apple’s own built-in ones. If apps are generating their own metadata, this can also be queried. To see the full mdfind schema, use:

 % mdimport -X | less

Yes, you want to run it through a pager because it’s a looooong JSON file.

Coda

Sometimes, though, we need to head up out of the two dimensional world of the command line. While regex is fine for finding files where you don’t quite remember their names, a far better and intuitive way is to use fuzzy search.

For me, looking for files to edit starts actually with launching an instance of vim then using the fuzzy file finder called telescope.

And what about on Linux? Well, Ubuntu has a Spotlight-like implementation called lens that has been available in Unity for some time now.