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.
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.)
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
yum package managers:
apt install locate
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:
If you are not the root user, you will need to use
sudo to update the database:
How to Search With
locate is as simple as invoking the program followed by your search pattern:
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
And, if you know them, and you can even more specific with regular expressions. (See below for options.)
To extend the
locate utility, there are a number of different options that can be helpful:
- Make your search pattern case insensitive.
- Use a regular expression in your pattern.
- 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?
Thoughts on “How To Use The `locate` Utility In Linux”
“yum install locate” throws and error – can’t find module yummain …
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.