Basic Linux Commands: The locate Command

Basic Linux Commands: The locate Command
Page content

What locate does?

As you would expect, the locate command searches for files within the Linux directory structure. And similar to the Google desktop search, locate searches through a database that is created with a secondary command updatedb. This file, after updated, is compressed with incremental encoding so the search process is faster. Because locate requires a database to search, it is important to make sure you update the database in order to include files that have been added since the last database update (more on this later).

Basic usage

One important aspect of locate is that a standard user will see similar results as the root user. Even though the standard user can see the files, they still can not execute or write to any file they do not have the proper permissions for. So if the standard user issues a search for httpd.conf they will see the /etc/httpd/conf/httpd.conf file because the permissions on the file are -rw-r–r– (which means the standard user has read permission). So the results of the search for httpd.conf (regardless of user) will look like this:

/httpd.conf_/_

etc/httpd/conf/httpd.conf

/etc/httpd/conf/httpd.conf.save

But how did we come to this output? Simple. By issuing the command locate httpd.conf.

Options

There are a couple of useful options for the locate command. The first is the “-c” option which will only return a count of the number of matches for the search string. In the above option, if the user issued locate -c http.conf the results would be simply “4”. This is handy when you need to know how many copies or iterations of a certain file are within your system hierarchy.

Another useful option is the “-i” option which will ignore case. This is helpful when you are not sure of the case of the file name (remember Linux is almost always case sensitive.) With this you could issue the command locate -i HTTPD.CONF and you will see the same results as if you ran the command locate httpd.conf.

Keeping the database updated

Because locate depends upon an updated database it is always smart to keep your database updated. This is done with the updatedb command and must be run as the root user (or using sudo if in a Debian based distribution). If you do a lot of searching it is best to make sure the updatedb command is issued regularly.

Final Thoughts

There are many tools to help you search. There are graphical tools and command line tools. As simple as the graphical tools can be, they can not match the power of the command line search tool locate.

This post is part of the series: Keeping Your Linux Machine Clean

If you have used a PC for any length of time you know that a hard drive can very quickly become cluttered with files, remains of uninstalled applications, and disorganized files and directories. In this Brighthub series you will learn the tools to keep your Linux system clean.

  1. Keep Your Linux Box Clean with These Tools
  2. Linux Command Line: cruft-remover
  3. Linux Command Line: history
  4. Linux Command Line: rm
  5. Linux Command Line: locate