Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

ChrisDolan (2855)

ChrisDolan
  (email not shown publicly)
http://www.chrisdolan.net/

Journal of ChrisDolan (2855)

Sunday July 26, 2009
02:08 PM

[non-perl] mdfind vs. locate

[ #39355 ]

I've used the "locate" command for many, many years on various POSIX systems. It's a very fast search for filenames containing the specified string on your machine.

Here's a Mac command that simulates "locate" using the Spotlight database:

#!/bin/sh
mdfind "kMDItemFSName == '*$1*'"

It's not as fast as locate, but it is always up to date (as opposed to locate, which relies on cron to reindex the disk periodically). As anyone can guess from the syntax, it's also very flexible. To find all files that originated as email attachments from my friend Glenn, for example, I can do:

mdfind "kMDItemWhereFroms == '*medianinja.com*'"

Or to find all files bigger than about a gigabyte:

mdfind "kMDItemFSSize > 1000000000"

(the WhereFroms search is fast and the size search is dramatically slower, maybe because all files have sizes but not many have kMDItemWhereFroms metadata)

To get a list of the supported keywords, type "mdimport -A"

Update: I found the Spotlight query syntax to be useful

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Here's an article about mdfind I wrote three years ago: http://www.oreillynet.com/pub/a/mac/2006/01/04/mdfind.html [oreillynet.com].

    There's actual Perl code at the end to find the longest songs in my iTunes collection:

    $ perl longsongs 9
    20:34 "2112" by Rush from "2112"
    18:36 "Alice's Restaurant Massacree" by Arlo
        Guthrie from "The Best Of Arlo Guthrie"
    ...
    9:16 "Between I And Thou" by The Mermen from
        "A Glorious Lethal Euphoria"
    9:05 "Watermelon In Easter Hay" by Frank Zappa
        from "Joe's

    --

    --
    xoa