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?
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 thanfind
too.As much as I like Oli (which is a lot!) I disagree with him on the
find
command. I don't like it.find
command takes over three minutesTake for example this simple command:
It takes over three minutes for
find
to search everything starting from/
. By default reams of error messages appear and you must search through them to find what you are looking for. Still it is better thangrep
to search the whole drive for a string which takes 53 hours: `grep`ing all files for a string takes a long timeI know I can fiddle with the find command's parameters to make it work better but the point here is the amount of time it takes to run.
locate
command takes less than a secondNow let's use
locate
:The locate command takes less than a second!
updatedb
only run once a day by defaultIt is true the
updatedb
command which updates the locate database is only run once a day by default. You can run it manually before searching for files just added by using:Although this will take 3 seconds, it's small in comparison to
find
command's 3+ minutes.I've updated my
sudo crontab -e
to include the line at the bottom:Now every five minutes
updatedb
is run andlocate
commands database is almost always up-to-date.But there are no attributes?
You can pipe
locate
output to other commands. If for example you want the file attributes you can use:Summary
I posted this answer to show the speed and ease of use of
locate
. I tried to address some of the command short-comings pointed out by others.The
find
command needs to traverse the entire directory structure to find files. Thelocate
command has it's own database which gives it lightning speed in comparison.