How to locate a file in Spotlight using folder and file name?

I often want to navigate to a file or folder that does not have a unique name. Instead, the parent folder in combination with the file or foldern name makes it unique. Here are a few examples:

  • I want to open readme.txt in the folder called ABC rather than readme.txt in the folder called CDE or FGH.
  • Each year I teach a subject where the folders are organised subjectname/2011, subjectname/2012, etc. I want to be able to search subjectname 2012 because 2012 is not unique.

When i enter a part of the path and the filename into Spotlight, the file is not found. Spotlight does not seem to treat path information as relevant to the search.

  • What is a quick way of navigating to a file using spotlight when its name is not unique but you know a word or partial word that appears in its path?
  • Alternatively, is there another strategy for quickly locating a file or folder by name and a word in the file or folder's path?

Update, I have read that Spotlight treats the path in a special way. It can't be used in constructing searches.

kMDItemPath
Complete path to the file. This value of this attribute can be retrieved, 
but can't be used in a query or to sort search results. This attribute can’t 
be used as a member of the valueListAttrs array parameter for MDQueryCreate 
or MDQueryCreateSubset.
Value Type:      CFString
Framework:       CoreServices/CoreServices.h
Header:      MDItem.h
Availability:    Available in OS X v10.4 and later.

If you're comfortable opening the terminal you could run the command

locate ABC/readme.txt

and it will give you the full file path to any files on your machine that are named readme.txt in a folder named ABC.

While you're there you can copy and past the path as an argument to the open command and it will open it for you.

Taking it one step further you can use the output of locate as the arguments for open with xargs

locate ABC/readme.txt | xargs open

Will open all files that have ABC/readme.txt on your machine just as if you had double-clicked them.


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 the find command but at a similar speed to mdfind.
  • 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.


You may want to check out Findspot. Findspot supports full path searching so you can include the folder name and filename in your query.

Here is a screenshot of Findspot using your example:

enter image description here


I've been looking for the same thing for many years. I know there are tons of creat command line tools, but wanted something to basically replace Spotlight, especially since Spotlight has gotten even worse in Big Sur. I finally just came across GoToFile, which seems to do exactly what you're asking for!