2

The locate command doesn't list any search results from files that are in the hierarchy of my home folder. Is there a config file to tweak to enable this behavior? I've tried:

sudo locate word
sudo /usr/libexec/locate.updatedb
chmod 755 ~ (and 777)

and restarting the shell. Still no luck. MacBook Pro, Sequoia, bash 3.2.57. Thanks!

7
  • You shouldn't need sudo for locate. If a file is not in the index, it's because locate.updatedb didn't add it. You could try to do chmod 755 ~ first, and run sudo /usr/libexec/locate.updatedb afterwards, but even that will not index files in ~/Documents etc (unless you chmod those directories as well).
    – nohillside
    Commented Nov 30 at 16:20
  • I asked a similar (identical?) question: apple.stackexchange.com/questions/475449/…
    – luckman212
    Commented Dec 1 at 16:53
  • Welcome to Ask Different. Have you given the Terminal app access to ~/Documents and ~/Desktop in System Settings > Privacy & Security?
    – jaume
    Commented Dec 4 at 15:49
  • @jaume Terminal has Full Disk Access, is there anywhere else to be more specific? Commented Dec 5 at 17:32
  • 1
    @jaume The locate DB only included files accessible by user nobody. Usually the content of Documents etc isn’t readable by nobody.
    – nohillside
    Commented Dec 5 at 21:16

2 Answers 2

2

By default, the process to rebuild the locate database runs as user nobody. This prevents it from indexing the content of user directories (which would be a security risk on systems with more than one human user).

Rebuilding the index with sudo doesn't help. At the top of /usr/libexec/locate.updatedb, there is

if [ "$(id -u)" = "0" ]; then
#ifdef __APPLE__
        rc=0
        export FCODES=`sudo -u nobody mktemp -t updatedb`
        chown nobody $FCODES
        tmpdb=`su -fm nobody -c "$0"` || rc=1
        if [ $rc = 0 ]; then
                install -m 0444 -o nobody -g wheel $FCODES /var/db/locate.database
        fi
        rm $FCODES
        exit $rc
#else
#       echo ">>> WARNING" 1>&2
#       echo ">>> Executing updatedb as root.  This WILL reveal all filenames" 1>&2
#       echo ">>> on your machine to all login users, which is a security risk." 1>&2
#endif
fi

This checks whether the command is run as root and, if yes, reruns it as user nobody (the tmpdb=... part).

So, if you want to index your home directory, you need to create a copy of /usr/libexec/locate.updatedb and remove the whole section. Also, you need to disable the default launch daemon and set up your own in /Library/LaunchDaemons.

-1

The update process runs as the unprivileged user nobody, even if you start it as root, so it only has access to the top of level of user home folders, unless the permissions have been hacked. This is for good reason. locate should not be used for this purpose. That's what Spotlight is for. If Spotlight doesn't meet your needs, I suggest you search the site for a solution, and post another question if you don't find one.

8
  • Not sure if there is a good reason to prevent locate.updatedb from indexing user files if there is only one human using a specific Mac.
    – nohillside
    Commented Nov 30 at 20:26
  • I think there is a good reason, and it's the reason why Spotlight goes to great lengths to safeguard privacy, which locate does not. My answer reflects my opinion, not yours. Try reading the locate.updatedb script. In any case, it doesn't matter, because locate is not going to index user files anyway.
    – Linc Davis
    Commented Nov 30 at 20:35
  • Answers based on facts are prefered over opinions. I know the reason. But telling people "this is for good reason" without explaining it, and telling them to search for another solution if they don't like the proposed one isn't all that helpful in a answer.
    – nohillside
    Commented Nov 30 at 20:38
  • 1
    Not below the top level, unless you put wide-open read permissions on everything, and even then, not folders restricted by TCC, such as Documents. You’re beating your head against a wall here. If you don’t like Spotlight, there are other options.
    – Linc Davis
    Commented Nov 30 at 23:24
  • 1
    Just for context, I'm just a dude playing around on a computer that no one else is ever going to use and has no sensitive data on it. I'm trying to learn some command line and shell scripting stuff and I just thought it would be handy to be able to pound out a locate command if I wanted to. I'm not opposed to Spotlight, but academically I was also interested in understanding why I was having trouble with something that seemed it should be straightforward. Commented Dec 5 at 17:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .