Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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.
share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

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.

share|improve this answer
    
+1 Thanks. I knew about find but not the locate command. –  Jeromy Anglim Nov 13 '12 at 6:57
1  
One thing I should add, locate is driven from the locatedb which needs to be periodically updated. Apparently this maintenance has been removed from the scripts that it ran in prior to 10.6, you may need to enable it like this sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist osxdaily.com/2011/11/02/… –  nine9ths Nov 13 '12 at 7:05
    
Yes. The first time I tried to run locate on Lion it prompted me to run the command you mention. –  Jeromy Anglim Nov 13 '12 at 7:20
add comment

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.

share|improve this answer
1  
Note, there is no single file requirement for xargs open you can pipe a list of files and it will open every one. This can be useful or dangerous. You can also specify an application to open them with (if it doesn't already have a command line equivalent) with open -a appname –  nine9ths Nov 13 '12 at 16:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.