The locate command
Need to find a file fast on a Unix, Linux, or Mac filesystem? Use the locate command.
Background
To find files on Unix systems, I've historically fired up my old friend the find command. For instance, to find a file named tomcat.sh, I used to type something like this:
find / -name tomcat.sh -type f
This is a lot of typing, and although the results are very current, it takes a long time to run on a big system. Recently a friend was kind enough to tell me about the locate command, and I haven't looked back since.
Using locate
Using locate is easy. Just type locate, followed by the name of the file you're looking for, like this:
locate tomcat.sh
Or, add the -i option to perform a case-insensitive search, like this:
locate -i springframework
Your Linux system will very quickly tell you all of the places it has been able to find (locate) the file with the name you specified. I emphasize the "very quickly" part, because this is so much faster than using the find
command you'll never look back easier. And, it's a heck of a lot easier to type.
Another thing to note: if you type in a name like foo (or any other name) locate, by default, returns the name of any file on the system that contains the string "foo", whether. So whether the filename is foo, or foobar, or barfoo,
locate will return all of these as matches.
How it works
The locate command works so fast because it runs a background process to cache the location of files in your filesystem. Then, when you want to find the file you're looking for, you can just use the command like I showed previously. It's that easy.
I'm told that occasionally locate may not be able to find a file, in particular if the file is very new and has not been put in the cache yet. However, that hasn't happened yet, and when it does, I can always use my trusty find command. Until then ... it's locate for me.