Is thre a way to find recent files with locate OR automatically update the database each time a file is created

command linelaunchdterminal

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.plist
to 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

Best Answer

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.