Need to find a file fast on a Linux filesystem? Use the Linux locate command.
To find files on Unix and Linux 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 told me about the Linux locate command, and I haven't looked back since.
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.
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. Here's a quick blurb from the locate man page:
The locate program may fail to list some files that are present, or may list files that have been removed from the system. This is because locate only reports files that are present in the database, which is typically only regenerated once a week by the /etc/periodic/weekly/310.locate script. Use find(1) to locate files that are of a more transitory nature. The locate database is typically built by user "nobody" and the locate.updatedb(8) utility skips directories which are not read-able for user "nobody", group "nobody", or world. For example, if your HOME directory is not world-readable, none of your files are in the database.
So, as the man page states, if you can't find a file using the locate command, it may be that the database is out of date, and when this happens, you can always use the find command. The find command will be slower because it scans all the files you specify in real time (it doesn't have a database backing it up), but you can also perform more powerful searches with it.
Post new comment