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

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


Solution 1:

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.

Solution 2:

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.