Thank you to @nine9ths for pointing out the locate
command as one option. Here are a few additional observations.
Partial matching and multiple search terms
I often have folders where I can only partially remember names of files and folders. And the folders may not necessarily be in the immediate parent folder (i.e., it might be the parent of the parent folder). One way of performing a boolean type partial matching search is to combine piped grep
commands. For example:
locate AB | grep readme
would pipe (|
) all paths that match AB into grep
. The remaining paths would also match readme
. Thus, this would match ABC/foo/readme.txt
, ABC/foo/readme.md
and ABC/readme.txt
.
Case insensitive search
Spotlight search appears to be case insensitive which generally seems convenient. For the same behaviour with locate
and grep
use the -i
flag.
locate -i AB | grep -i readme
grep
commands can be repeatedly applied to further filter the results.
Selecting a path from a list
In some cases, even after several greps, I find that I can still be left with a handful of matches and I just want to select one of them from the list. There are various ways of selecting a single line by number. Here a couple of options for selecting for example line 4 of some list of paths from a locate search
locate AB | grep readme | awk 'NR==4'
Opening a selected path
As @nineths notes once a single file is obtained, the output can then be opened with a default application:
locate foo | xargs open
or with a specified program on the path such as Vim
locate foo | xargs mvim
xargs
does not work, at least by default, if the path includes a space.
Another option is to send the path to the clipboard and paste it somewhere (e.g., into an Alfred search or a Finder Go to folder dialog box).
locate foo | pbcopy
Alternative to command-line selection and opening
Presumably there are several alternatives to using the command-line to select and open a path from the list of returned paths.
- In iTerm2 holding the command key and clicking a URL or file/folder path will open the URL or file in the default application.
Customisation of locate
This tutorial from Geek Stuff discusses various customisations to the locate
command (e.g., filtering out certain directories and filetypes).
Potential to use mdfind instead
In general all the examples using locate above could be replaced with mdfind
.
locate
generally references additional system files whereas mdfind
accesses those files indexed by Spotlight. mdfind
is case insensitive by default, which I generally find convenient. mdfind
also has additional search options e.g., you can add kind:fol
for folders. The main difference is that mdfind
only searches the file name. Thus, if path information is being used to limit the search, then that must appear in the subsequent piped grep
filters.
For example, if I wanted to find a the folder called 2012
where subjectname
appears in the path the following would work
mdfind name:2012 kind:fol | grep subjectname
Note that name:
seemed to be required because the text was all numbers.
Summary thoughts
- Speed: The
locate
command runs faster than thefind
command but at a similar speed tomdfind
. - Ability to filter: If you have information to partially match on the file name or you want to use additional search tools
mdfind
seems better (assuming the file is indexed by Spotlight).
In general all these approaches seem a little bit fiddly and have a fair amount of unnecessary syntactical overhead.