Locate command can't find anything inside Documents folder on mac
Asked Answered
E

3

7

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

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

I then try and search for something inside my Documents folder

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

I then try another random folder:

Documents $ cd ..
~ $ 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
~ $ 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.

Euchre answered 8/4, 2013 at 19:37 Comment(0)
R
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).

Rogers answered 8/4, 2013 at 20:13 Comment(5)
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?Euchre
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 for more info.Mckoy
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).Drilling
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).Euchre
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.Uigur
W
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.

Walcott answered 8/4, 2013 at 20:1 Comment(0)
D
1

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

Dismember answered 9/11, 2016 at 14:44 Comment(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.Euchre

© 2022 - 2024 — McMap. All rights reserved.