How To Use The `locate` Utility In Linux

How to locate files in Linux with locate

Do you need to locate a specific file in your file system? Over many years of managing your server, you will likely find that working on the command line gives you flexibility, comfort, and speed in attending to your regular tasks. And this is very well demonstrated in the various search functions that come with most Linux distributions. Whether you are using Linux in your remote server or as your local operating system, command line searching with the locate utility is easy and fast.

How locate Works

One of the primary benefits of the locate utitlity is its blazing fast speed. How does it work so fast?

Locate works quickly by checking a database that builds itself in your system every day. This creates a massive index of every file in your system. However, you may have newly created files that are not present in the index. (See the section below on how to update the database manually, if you require it.)

Making Sure locate Is Installed

Depending on your Linux distribution, you may or may not have locate bundled in with your suite of software. If you find it is not installed, you can use the apt or yum package managers:

apt install locate

Or,

yum install locate

Once it is installed, you will need to make sure that there is content in the database for locate to search through. Otherwise, you might see the following error:

locate: can not stat () `/var/lib/mlocate/mlocate.db': No such file or directory

To correct this, you can run the updatedb command as follows:

updatedb

If you are not the root user, you will need to use sudo to update the database:

sudo updatedb

How to Search With locate

Searching with locate is as simple as invoking the program followed by your search pattern:

locate <filename>

You may notice there are far more results than you can use. So you can refine your search using a wildcard. For example, to search only files that end with a .txt extension:

locate *.txt

And, if you know them, and you can even more specific with regular expressions. (See below for options.)

Useful Options

To extend the locate utility, there are a number of different options that can be helpful:

-i
Make your search pattern case insensitive.
-r
Use a regular expression in your pattern.
-c
Instead of printing results, provide a number of results. This way, without having to fill your screen with results, you can get an idea of how your search goes, and if more refinement may be necessary to get usable results.

Where to stop next on your cloud server management journey?

CM
Christopher Maiorana Content Writer II

Christopher Maiorana joined the InMotion community team in 2015 and regularly dispenses tips and tricks in the Support Center, Community Q&A, and the InMotion Hosting Blog.

More Articles by Christopher

2 thoughts on “How To Use The `locate` Utility In Linux

    1. Hello Pete – Apologies for the delay in answering your concern. The “LOCATE” command is typically installed as “MLOCATE” in CENTOS. You can see more about how the command is installed in this article . Remember, you will need to have ROOT access to install this command. If you’re still having issues with it, then you should consult with our live technical support team for further assistance. They can access your server to further investigate the issue.

Was this article helpful? Join the conversation!