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

Comments

It looks like this article doesn't have any comments yet - you can be the first. If you have any comments or questions, start the conversation!

Was this article helpful? Let us know!