Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.4: Use Spotlight to create a better locate UNIX
Tiger only hintI often use the find or the locate commands in Terminal to search for stuff scattered all over my file system. They both work well, however, both also have drawbacks. locate is nice and fast, as it searches a pre-built database, however this database isn't kept updated on a regular basis -- it's rebuilt once a week by the /etc/weekly script, if your computer is on at 3AM. As a result, locate will miss newly created or downloaded files.

find is great -- it walks your filesystem to find things, however, this process isn't exactly fast if you've got half-a-million files on your drive. find> also has some very useful features for doing things to the found files, but that's a topic for another hint on its own.

Not wanting to get rid of the funtionality of find, locate is an ideal candidate for replacement as it purely searches its database for filenames. Add the following snippet to your .bashrc file, and it will redefine the locate command to search the Spotlight database rather than /var/db/locate.database.
# using the new md* commands in Tiger, this is a find, based on filename,
# using the spotlight database. No having to build the locate database
# and keep it up to date, or run the usual find that walks the filesystem
# It's done as a function so I can use command-line arguments
# and single and double quotes.

function locate {  mdfind "kMDItemDisplayName == '$@'wc"; }
There are many other kMDItem parameters you can also search on that have been referred to in other hints. For completeness' sake, they're located in this Spotlight Metadata Attributes article on Apple's Developer site.

[robg adds: Instead of giving up a built-in command, I'd recommend just creating a new command -- I changed it to function l2 {..., and can now just type l2 what_to_find to run this "Spotlight-enhanced" locate command.]
    •    
  • Currently 2.67 / 5
  You rated: 1 / 5 (6 votes cast)
 
[14,146 views]  

10.4: Use Spotlight to create a better locate | 15 comments | Create New Account
Click here to return to the '10.4: Use Spotlight to create a better locate' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.4: Use Spotlight to create a better locate
Authored by: noumenon on May 17, '05 10:57:05AM

isn't this just what "mdfind" does? i thought apple already built this in.



[ Reply to This | # ]
mdfind
Authored by: lullabud on May 17, '05 02:17:16PM

If you'll notice, this is USING mdfind. mdfind searches all of the metadata though, and locate only searches filenames, which is what this example mdfind command does.



[ Reply to This | # ]
10.4: Use Spotlight to create a better locate
Authored by: kirkmc on May 17, '05 11:04:50AM

The locate database includes all files on your Mac; this is not the case for the Spotlight indexes.

Just use mdfind, if you want, but it won't find all those tasty config files or executables.

---
Read my blog: Kirkville -- http://www.mcelhearn.com
Musings, Opinion and Miscellanea, on Macs, iPods and more



[ Reply to This | # ]
Searches Fink
Authored by: lullabud on May 17, '05 02:21:27PM

Although it doesn't search most hidden unixy trees, I just noticed that it searches /sw by default. And, of course there are ways to force spotlight to search those extra trees.



[ Reply to This | # ]
Searches Fink
Authored by: bjast on Aug 16, '05 10:00:45AM

What are the 'ways' to search hidden unixy trees?

I'd like to find things like:

.bashrc
.zshrc

Thanks,
bjast



[ Reply to This | # ]
10.4: Use Spotlight to create a better locate
Authored by: Dragon76 on May 17, '05 02:27:55PM

How do you create the function in tcsh?



[ Reply to This | # ]
10.4: Use Spotlight to create a better locate
Authored by: vramin on May 17, '05 06:01:46PM

You don't need to create a function.

Just put the following code into your .cshrc or your .tcshrc file:

alias myfind mdfind "kMDItemDisplayName == '!'wc"

Then at the command prompt type the following to reset your environment with the myfind command:

source $HOME/.cshrc;rehash


btw - You don't have to call it myfind. Just try not to name it "ls" or "rm" or the like.



[ Reply to This | # ]
10.4: Use Spotlight to create a better locate
Authored by: vramin on May 17, '05 07:14:34PM

You might want to add the alias:

alias redo "source $HOME/.cshrc;rehash"

Then when you change your .tcshrc or .cshrc file you can type "redo" instead of
"source $HOME/.cshrc;rehash".



[ Reply to This | # ]
10.4: Use Spotlight to create a better locate
Authored by: jtrott on May 17, '05 11:28:15PM

You may want to instead use kMDItemFSName as the attribute to search on. Otherwise you will get item names (like email subjects that match) as well as finder file names.
kMDItemFSName will just return file system names.



[ Reply to This | # ]
10.4: Use Spotlight to create a better locate
Authored by: ammon on May 18, '05 08:15:29PM

kMDItemFSName would be more correct, but unfortunately I find that it is much slower than kMDItemDisplayName. This is unfortunate because uness you turn "Show all file extensions" on in the Finder, your searches can't include the extension.



[ Reply to This | # ]
find system files?
Authored by: pkretek on May 18, '05 02:28:16AM

Is there any hint/hack/tip on how to add system directories into spotlight? It does not index all the /private/*, /usr, /bin... files like locate / find



[ Reply to This | # ]
YES. was: find system files?
Authored by: pkretek on May 18, '05 02:50:30AM

I should have searched before posting :p

Configure Spotlight to index excluded directories
http://www.macosxhints.com/article.php?story=2005050222125145

sudo vi /.Spotlight-V100/_rules.plist

Replace:
<key>INCLUDE</key>
<array/>

With:
<key>INCLUDE</key>
<string>/private</string>
<string>/usr</string>

And run:
sudo mdutil -E /



[ Reply to This | # ]
10.4: Use Spotlight to create a better locate
Authored by: stewby on May 20, '05 03:16:11AM

locate is an ideal candidate for replacement as it purely searches its database for filenames.

That is not true. locate searches by path, not filename, so you lose functionality by replacing it with mdfind.



[ Reply to This | # ]
10.4: Use Spotlight to create a better locate
Authored by: bjast on May 22, '05 09:00:36AM

I might have missed something, but using the initial suggestion (and Rob's l2 suggestion) I'm not finding docs & dirs like:

/private/etc/profile

Where % locate profile finds docs & dirs like:

/private/etc/profile
/private/var/vm/app_profile
/private/var/vm/app_profile/0_data

Is there a way to modify this helpful hint to show dirs & docs like the above?

Thanks,
bjast



[ Reply to This | # ]
10.4: Use Spotlight to create a better locate
Authored by: david-bo on Apr 28, '08 01:09:58PM

I would like to see a locate db update script that subscribed to FS-events and dynamically updated the locate.db when files where created/removed/renamed.

---
http://www.google.com/search?as_q=%22Authored+by%3A+david-bo%22&num=10&hl=en&ie=ISO-8859-1&btnG=



[ Reply to This | # ]