2

I was recently trying to find a binary file with a certain name. I decided to use the whereis search utility (part of the util-linux package). This managed to find the file in half the time than trying to use the comparable find command.

I've also used the locate command which produces results in speeds similar to whereis.

How do these commands manage to find files so quickly in comparison to the find command?

  • locate reads from a database that is updated by a much slower, CPU-intensive search (updatedb), typically run as a cron job... – jasonwryan Nov 16 '15 at 21:00

1 Answers1

4

find does a blind walk of the entire filesystem looking for matches. It takes so long because it's looking at every file in every directory and not stopping until it's covered the whole filesystem.

locate also covers the whole filesystem but it speeds things up by having a prebuilt database (updated with the updatedb command) that it uses to find matching files.

whereis and which also search through directories looking for files but they only search through a very specific subset of places in the filesystem (usually specified by environment variables like $PATH and $MANPATH as appropriate). They also stop a the first occurrence leading to faster run times.

David King
  • 3,147
  • 9
  • 23