mac - Which tool is most flexible for searching my whole system, locate or mdfind?

06
2014-04
  • Bill McCloskey

    Follow-on to Spotlight and the locate command don't search *all* folders:

    The steps mentioned by Gordon Davisson were performed; .bashrc was entered in the Spotlight search field, and System and Invisible files were enabled as per the above reference. The .bashrc file didn't show up at all. The ${HOME} perm's were changed from 750 to 755 and several hours elapsed, but .bashrc still did not show up.

    What is required to see .bashrc with Spotlight? How can Spotlight display .bashrc everywhere, if at all, to see the most recentt one, as in?:

    me@My-MacBook-Pro ~
    $ locate .bashrc | most_recent_file
    /Users/me/.bashrc
    

    (most_recent_file is a Perl script which stat's each file in a list and prints the name of the newest one):

    Note: The locate.updatedb database creation script was changed to allow find to search the /etc/locate.rc specified volume(s) as root or nobody, depending on whether root or daemon invokes it, as per What folders are indexed / covered by 'locate'). The logic is tricky, so here's the comment for the updated /usr/libexec/locate.updatedb:

    # Modify test for expected invocations by either daemon (id=1) or root (id=0);
    # if invoked as root, skip test section and search filesystems as root as per
    # /etc/locate.rc, a possible security risk if /etc/locate.rc is not tailored
    # for production use.  Invoked as daemon, we "spawn" ourselves as nobody to
    # gain nobody's filesystem visability, rather than daemon's.
    #if [ "$(id -u)" = "0" ]; then
    if [ "$(id -u)" = "1" ]; then
    

    Here is the Perl source for the most_recent_file.pl script. We have a symbolic link most_recent_file in our search paths.

    #!/usr/bin/perl -wnl
    # From pathname inputs, emits name of one most recently modified
    # Gives correct answer where pipelines of this form may not:
    #   find . -print | xargs ls –lrdt | tail -1
    
    # NOTE: Use find or locate to provide input, or ls -d dir/*,
    # but *not* simply "ls dir" (dir won't be present in pathname)
    
    # Sample invocations:
    #      locate '*.c' | most_recent_file
    #      ls -d /etc/* | most_recent_file
    #      find /local -name 'somescript' | most_recent_file
    #      most_recent_file < filelist
    
    BEGIN {
        $newest = 0;    # initialize modification-time reference point
        $name   = "";
    }
    
    # Get file's numeric modification time; 10th value from stat
    $mtime = ( stat $_ )[9];    # indexing into output of stat
    if ( $mtime > $newest ) {   # if True, current file is newest yet seen
    
        # Remember mod-time for comparison to others,
        # and remember filename for final report
        $newest = $mtime;
        $name   = $_;
    }
    
    END {
        print $name;
    }
    
  • Answers
  • Gordon Davisson

    I haven't been able to get the Finder's spotlight search to list .bashrc either (perhaps it's finding it, but not displaying because it's invisible?). But the command-line interface to spotlight... well, it can be convinced to show it. This works:

    $ mdfind kMDItemFSName = ".bashrc"
    /Users/gordon/.bashrc
    

    But for some reason the -name option doesn't show it:

    $ mdfind -name ".bashrc"
    $
    

    ...so I don't completely trust this to find what I think it should.


  • Related Question

    file - locate/updatedb like tool (Linux CLI) for CDs/DVDs
  • yogan

    I am looking for a Linux tool to search for files on offline media like CDs and DVDs. I know there are a lot of "cataloging" tools, but those are all graphical and I want a command line program. Ideal would be if works just like the well-known locate/updatedb bundle, with the only exception that the db update would have to be interactive, so that you can enter some id or description to a media, which will be printed by the locate-like tool, e.g.

    $ medialocate foo.png
    MEDIA_DVD_01:/pictures/misc/foo.png
    MEDIA_DVD_08:/photos/foo.png
    MEDIA_DVD_08:/img/bar/foofoo.png
    

    Does such a tool exist yet? I haven't found anything, but this probably due to the fact that terms like "locale"/"cd"/"files"/"catalog" are just too common to yield good results. It wouldn't be too hard to write something like that myself, but I don't want to reinvent the wheel here.


  • Related Answers
  • Vinko Vrsalovic

    You can use updatedb to have it parse the removable media paths, just configure accordingly the PRUNEPATHS and PRUNEFS environment variables in /etc/updatedb.conf or equivalent. Although this will only remember the path so if you mount in the same directory a different media and have updatedb run, it'll overwrite (or append?) the files.

    You can workaround this by mounting each of your CDs to be catalogued in its own directory, not very hard, but a bit of a nuisance.

  • Juliano

    My particular solution to that is to use isoinfo from wodim to generate files in a ~/dvddb/ directory, with the label of the DVD as the filename (if you don't use RockRidge extensions, change -lR to -lJ):

    isoinfo -lR -i /dev/scd0 > ~/dvddb/xxx.txt
    

    Then, to find some file:

    grep -i 'name' ~/dvddb/*
    

    You may want to create shell aliases.

  • Aiden Bell

    I use a shell script along the lines of:

    #!/bin/bash
    find $1 -type f -exec xmler.sh {} + > out.xml
    

    Where xmler creates:

    <file>
        <filename>someFilename.txt</filename>
        <md5sum>xxxxxxxxxxxx</md5sum>
        <date_modified>....</date_modified>
    </file>
    

    The file entries get wrapped in

    <catalogue type="dvd" diskid="someid"></catalogue>
    

    and so on, into an XML document. I can then write tools to create HTML catalogues and indexes, search tools and more.

    A bit hackish, but works for me.