Ubuntu – find vs. locate

findlocate

There are the commands find and locate to search for files on the disk.

I know that find recursively processes all needed subdirectories to search files and therefore is slow but up-to-date, whereas locate uses a database that gets updated every now and then (when exactly?) to quickly show results which might be outdated though.

Are there any other differences? In which situations would one prefer the one or the other? And when does the locate database get updated usually?

Best Answer

locate is really only good for finding files and displaying them to humans. You can do a few things with it, but I wouldn't trust it enough to parse and —as you say— it's impossible to guarantee the state of the internal database, more so because it's only scheduled to run from /etc/cron.daily/mlocate, once a day!

find is live. It filters, excludes, executes. It's suitable for parsing. It can output relative paths. It can output full paths. It can do things based on attributes, not just names.

locate certainly has a place in my toolbox but it's usually right at the bottom as a last-ditch effort to find something. It's easier than find too.