0

I am on MacOS Catalina. locate is a great command but unfortunately, I have difficulties to find files which have been created for example one hour ago or ten minutes ago.

It seems that I could circumvent this issue by updating the database with a higher frequency than every 24 hours.

But the other problem is that, when I launch the command /usr/libexec/locate.updatedb in root, it lasts much time before this command ends.

So I conclude that update rebuilds completly the database and doesn't update only the new files created from last locate.updateb execution : indeed, this would be a big gain of time if the command was not rebuilding all the database with all the files of the system.

Finally, I think about a simple strategy : modidy /System/Library/LaunchDaemons/com.apple.locate.plistto force it to update for example the database every 5 minutes. But if the command /usr/libexec/locate.updatedb rebuilds all the database every 5 minutes, this doesn't make sense.

So, I would like to get feedback from people who have this kind of problem and how they solved it. I prefer to avoid using the command mdfind but if there is not available solution with locate and not the possibility not to have to rebuild all content each time (in my case, this would be each five minutes), I would be obliged to switch to mdfind.

By the way, a simple crontab launching every 5 minutes the command /usr/libexec/locate.updatedb is also allowed, isn't it ?

You could also advise me to use find with -mtime flag but find is very slow (associated with parallel, it might be an alternative, I don't know, actually, I am a locate fan since I have used it a lot on Linux).

Any clue/remark/feedback are welcome,

Regards

  • To perhaps provide an alternative solution to your problem, what is your actual use case here? Why do you want to know which files were created most recently? – Jivan Pal Feb 10 at 19:53
  • 1
    It sounds like you're trying to reinvent Apple's Spotlight file-indexing system; why not just use Spotlight via the mdfind command? – Gordon Davisson Feb 10 at 23:46
  • Seeding locate for a small directory might be doable, but spotlight is the way forward even if you need to adjust it or learn how to search by name... – bmike Feb 10 at 23:50
2

I prefer to avoid using the command mdfind

Why is that? It solves your problem with no additional work. Getting locate to do update constantly is going to keep your Mac busy scanning the entire hard drive almost all the time. In the meantime,

mdfind -name filename.txt

Will produce the same result with no extra work by you or by your Mac. Since macOS keeps Spotlight up to date automatically, this will find files immediately after they're created. You could even use a really short shell script like this:

#!/bin/sh

if [ "$1" ==  "" ]; then
    echo What files do you want to find?
else
    mdfind -name $1
fi

Then alias locate or some other command to call the script.

| improve this answer | |
  • 1) mdfind is great, except that it does not search in some places, notably ~/Library/ which is where documents in iCloud are stored. 2) Another good use of mdfind is mdfind -onlyin "$PWD" -name FOO for when you want to find a file named "FOO" in a folder or sub-folder of the current directory. – TJ Luoma Feb 11 at 1:08
  • It does search those places, by filename at least. That's what locate would do, so it works for this question. If you use mdfind -name with the name of a file on iCloud Drive, it will find matches in ~/Library/Mobile Documents/com~apple~CloudDocs/. – Tom Harrington Feb 11 at 18:26
0

You seem to prefer a CLI solution. I'm a frequent user of the CLI in MacOS, but we must all live with the fact that Apple does not update many of their command line tools. For example, on my Macbook Pro running Mojave, the locate tool doesn't support the --version argument, but man locate has a date at the bottom that was about 14 years ago. So - the locate tool in Catalina is not the same locate you've used on Linux.

That said, my "answer" is more along the lines of a suggestion that you consider using the "smart folder" feature in Finder. It allows you to filter a variety of file and folder types, and show them in Finder's sidebar. This may suit your needs, or it may not. In either case, you now know that Catalina's locate tool is fairly old now.

Since my answer is off-topic I won't elaborate further, but there are some decent "How-To" guides for using smart folders available online if you're interested.

| improve this answer | |

You must log in to answer this question.

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