Take the 2-minute tour ×
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.

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
add comment

4 Answers

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.

E.g.:

$ 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
add comment

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
add comment

Try mdfind instead.

$ mdfind -onlyin $HOME iTunes

The md* utilities use the same database as Spotlight.

share|improve this answer
add comment

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

locate hosts | grep ^/etc

or:

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
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.