Use locate to quickly find files on Linux

Use locate to quickly find files on Linux

locate is a great way to quickly find files on your system. It searches using an index created using the updatedb utility so it can find files much more quickly than find can.

The downside is that it won’t look for files that have changed since the last time you’ve run updatedb. On most systems this runs every day, but you can run it manually if you need to find a recently made file.

Below is a forced example, pitting find at it’s worse searching through everything:


# time find / -name "firefox"
...
/usr/bin/firefox
/usr/lib/firefox-3.0/firefox
/var/abs/extra/firefox

real 1m3.834s
user 0m0.683s
sys 0m2.107s

# time locate firefox
/firefox

/var/lib/pacman/sync/extra/firefox-i18n-3.0.8-1/depends
/var/lib/pacman/sync/extra/firefox-i18n-3.0.8-1/desc

real 0m0.435s
user 0m0.417s
sys 0m0.007s

In this case, locate is much faster. It also spits out a good deal more results than find does, so piping it into less or grep can be rather handy.

When you know what you’re looking for or need up to date information on what files are where, find will typically be much faster:


# time find /usr/bin/ -name "firefox"
/usr/bin/firefox

real 0m0.008s
user 0m0.007s
sys 0m0.000s

Further Reading:

http://www.debian.org/doc/manuals/debian-tutorial/ch-file_tools.html

man find
man locate

Online versions of these two man pages can be found at:
http://unixhelp.ed.ac.uk/CGI/man-cgi?locate
http://unixhelp.ed.ac.uk/CGI/man-cgi?find