Linux locate command examples

Linux FAQ: Can you share some examples of how to use the Linux locate command?

Background

Sure. The locate command is used to find files by their filename. The locate command is lightning fast because there is a background process that runs on your system that continuously finds new files and stores them in a database. When you use the locate command, it then searches that database for the filename instead of searching your filesystem while you wait (which is what the find command does).

To find files on Unix and Linux systems, I've historically fired up my old friend, the Linux 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 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 the locate command

Using the command 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 Linux find command you'll never look back easier.

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 the Linux locate command 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. 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. For more information on the Linux locate command, please see the Linux locate command man page.

Post new comment

The content of this field is kept private and will not be shown publicly.