7

As it says in the description. I first check our database is up to date

mh547:bin crashandburn4$ sudo /usr/libexec/locate.updatedb
Password:       #password entered and function executed without errors

I then try and search for something inside my Documents folder

mh547:bin crashandburn4$ cd ~/Documents/
mh547:Documents crashandburn4$ ls
    Mamp_workspace/                         Scenarios.docx                          gc01/
mh547:Documents crashandburn4$ locate Scenarios.docx    #nothing returned

I then try another random folder:

mh547:Documents crashandburn4$ cd ..
mh547:~ crashandburn4$ ls
    Applications/                Movies/                      drawable/                    untitled-2.pdf
    Desktop/                     Music/                       drawable-xhdpi/              untitled-2.synctex.gz
    Documents/                   Pictures/                    dwhelper/                    untitled-2.tex
    Downloads/                   Public/                      linux_ssh*                   website-terminal-copy-paste
    Dropbox/                     Samsung/                     scripts/                     workspace/
    Google Drive/                Sites/                       untitled-2.aux               workspace_copy_to_linux*
    Library/                     android-sdks/                untitled-2.log
mh547:~ crashandburn4$ locate website-terminal-copy-paste 
/Users/crashandburn4/website-terminal-copy-paste #correct result returned

can anyone help me? I've been stuck on this for a good half hour or so.

Mike H-R
  • 7,726
  • 5
  • 43
  • 65

3 Answers3

17

As pointed out by plundra, that's because the locate OSX ships with is old and crippled and doesn't index and/or report files which are not readable by nobody, even when run as root. What you can do though is either install homebrew and then GNU locate, or, as suggested here use mdfind -name instead (I don't have an OSX box at hand to test this).

Adrian Frühwirth
  • 42,970
  • 10
  • 60
  • 71
  • 1
    ahh, so that's why, I'll update with homebrew, I'm not familiar with mdfind, I was using locate for speed reasons, having run mdfind it seems as fast if not faster, I don't suppose you know why that is? – Mike H-R Apr 08 '13 at 20:47
  • 2
    `mdfind` uses the Spotlight index -- a prebuilt database similar to locate's, except that it includes many file properties besides the name, and it's updated continuously. See [this blog post on SU](http://blog.superuser.com/2011/06/03/digging-deeper-mastering-spotlight-in-os-x/) for more info. – Gordon Davisson Apr 08 '13 at 21:01
  • 2
    According to the blog comment I linked `mdfind` uses Spotlight's index which is usually kept up to date, so you don't need to reindex manually. Thus I would not bother installing homebrew _only_ for an alternative `locate` implementation and stick to `mdfind` and add an alias (`alias locate=’mdfind -name’`). As for speed, since both use an indexing db they should be similarly fast - differences in speed can have lots of different reasons, from different db sizes, index format, sorting algorithm to optimization in the frontend tool (or having the _appearance_ that one is faster than the other). – Adrian Frühwirth Apr 08 '13 at 21:07
  • 1
    brilliant, thanks, very useful. for anyone else's reference you can easily update with macports or homebrew, the package name is findutils and (for me at least) while it could now search the Documents folder I would still recommend using mdfind as described above in terms of speed and effectiveness (there was a subfolder I was searching for that still couldn't be located with locate). – Mike H-R Apr 08 '13 at 21:29
  • Findutils with /opt/homebrew/opt/findutils/libexec/gnubin/updatedb and /opt/homebrew/opt/findutils/libexec/gnubin/locate would still not work for me, but I guess things might have changed in OS X in the last 10 years. – Marcus May 11 '23 at 16:23
9

It's because your Documents-folder isn't world readable, which is a good thing, specially on shared systems.

The BUGS section of the locate(1) man-page explains it:

The locate database is typically built by user ''nobody'' and the locate.updatedb(8) utility skips directories which are not readable for user ''nobody'', group ''nobody'', or world. For example, if your HOME directory is not world-readable, none of your files are in the database.

Try running ls -ld ~/Documents and you'll see the permissions. Wikipedia have an article on Unix permissions if you are unfamiliar with these.

plundra
  • 18,542
  • 3
  • 33
  • 27
1

You can do sudo ln -s /usr/libexec/locate.updatedb /usr/local/bin/updatedb to make the updatedb command available

  • 1
    while this is true, I think it is more useful to use `alias locate='mdfind -name'` if you want to use locate on a mac, as the inbuilt mdfind works well, and there's no reason to keep two metadata databases. – Mike H-R Nov 09 '16 at 15:41