Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to restrict locate output to specific directories on Mac OS X Snow Leopard? I read about using regex on a Ubuntu forum. However, on my mac locate does not accept -r option. I also looked at the man pages for locate on Mac and there is no -r option.

Thank you.

share|improve this question

Just use shell wildcards in the locate's search pattern - that functionality is a historical feature of the utility and should be supported on all systems.


$ locate '*/dirname/*filename*'

Be sure to use stars around the search string (or at least in front of it) as once you use wildcards locate switches the logic and anchors the search string to beginning and end of the searched material.

share|improve this answer

I think you need to install another version of locate. I do not have a OS X system to check, but it seems that slocate is present in macports, so it should be quite easy to install, and I think it does have the required option.

share|improve this answer

Try mdfind instead.

$ mdfind -onlyin $HOME iTunes

The md* utilities use the same database as Spotlight.

share|improve this answer

You can try to mix it with the grep command, like:

locate hosts | grep ^/etc


locate php | grep -e ^/etc -e ^/usr/local/etc

to look into multiple directories.

If you using this functionality very often, write a wrapper for it.

share|improve this answer

Your Answer


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.