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

2

1

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;
}

Billy McCloskey

Posted 2013-12-26T17:05:08.780

Reputation: 1 487

Answers

3

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.

Gordon Davisson

Posted 2013-12-26T17:05:08.780

Reputation: 28 538

2Doesn't seem to work on macOS Sierra (10.12) anymore. – Norswap – 2017-01-06T21:51:32.047

I changed the Finder attribute AppleShowAllFiles to true. I also set the spotlight Kind attribute File visibility to Visible or invisible. Neither of these yield a visible invisible file from spotlight like .bashrc. – Billy McCloskey – 2013-12-27T01:25:37.037