I know that locate
has to have a database generated and is much faster, and that find
does not need a database generated and is not as fast. So in what situations is find
and locate
more efficient/affective/give a better end result?

- 603
-
A duplicate of https://unix.stackexchange.com/q/60205/117549 – Jeff Schaller Dec 16 '17 at 03:23
-
@JeffSchaller I don’t think my question is exactly a duplicate, though there are similarities... While my question is asking the pros and cons, I am asking in what situations would either of the two command be more efficient and affective and give better results. – Hunter T. Dec 16 '17 at 04:45
2 Answers
As you said, locate has a database and is therefore much faster, while find has to traverse the filesystem. But locate's database only contains filenames (with their paths), while find can select files based on other meta data like mtime or size. So it is my resume versatile. Also the database has to be updated in a batch effort, usually nightly, so files which have been created modified recently do not appear correctly. Also you can expect every modern Unix system to have find available, while not all systems have locate's database enabled.
So, locate is faster, use it to find the location of a file, find is more versatile. There is no universal better.

- 486
-
I'm not an expert with either command by any means so take this with a grain of salt. However I believe another caveat of the locate database is that it will only include paths that the user who created it has read access. To me this means that even if run with
sudo
it may not find files that would be otherwise located withfind
. – jesse_b Dec 15 '17 at 23:15
Typically the locate
database is updated once per day. So if you need to search for anything that is newer than one day, you may need find
for that.
On the other hand, the larger the filesystem you need to search, the faster locate
will be over find
. Especially so if the filesystem is on a network share.

- 96,466
-
For others: the
mlocate
updatedb crontab is under/etc/cron.daily/mlocate
. – Rick Jun 04 '22 at 16:21