01. Searching for Files with locate locate

By now you'll probably be wondering - where is the search bar on the Linux Command Line?

Before we get started with the locate command, you need to set up a database that the shell can look for the files you are searching for.

Setting up a database to search from

If you run something like locate 'html' for the first time, you'll get an error message similar to below (Mac OS X).

WARNING: The locate database (/var/db/locate.database) does not exist.
To create the database, run the following command:
  sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist
Please be aware that the database can take some time to generate; once
the database has been created, this message will no longer appear.

If you're runnign Linux, you'll have an error such as this.

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

Both of these errors mean that you haven't created a locate database to store all your filenames. Without this, the shell has nowhere to look. Thus, simply run updatedb as the root user (if you're using Linux), or type in the command presented above (for Mac OS X). This will generate an indexed database for you. Note: it may take a while to generate, and will be running in the background, so just sit tight for a few minutes.

Updating your database

The database is recomputed daily, and contains the pathnames of all files which are publicly accessible. It runs as a cron job, which is a task that is run by the cron daemon periodically.

With this, you may find that some of your most recent files don't appear immediately with the locate file. Thus, to update your database manually, you can use the updatedb command. You may need to install the mlocate package.

The locate command

Once we have our database set up, we can use the locate command.

The locate command quickly searches in the database for a substring matching a specified pattern. The shell then displays every absolute pathname of the files found.

$ locate 'html'

This command would locate all files that start with the name "html" in all directories listed in our database.

We can also use wildcards to be more specific with what we want.

$ locate 'h*.html'
# List all .html files starting with an h.

To narrow our results further, we can pipeline the results with grep and specify the name of another folder.

$ locate '*.html' | grep share

This will locate all files with the .html extension that also has share in its path name.

Options with locate

Here are a few options that you may find helpful.

Suppress error messages that might arise from not having permissions.
Specify a number after to limit the number of outputs.
For case insensitive search.

You may find yourself limited to your search options with locate. Let's look at how we can do a more advanced search with the find command.

Take your Linux skills to the next level!

How Linux Works

Take your Linux skills to the next level! Try Linux & UNIX

In this completely revised second edition of the perennial best seller How Linux Works, author Brian Ward makes the concepts behind Linux internals accessible to anyone curious about the inner workings of the operating system. Inside, you'll find the kind of knowledge that normally comes from years of experience doing things the hard way.

$ Check price
39.9539.95Amazon 5 logo(114+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Acupressure Mat & Pillow

Aching back from coding all day? Try Back Problems

Relieve your stress, back, neck and sciatic pain through 1,782 acupuncture points for immediate neck pain relief. Made for lower, upper and mid chronic back pain treatment, and improves circulation, sleep, digestion and quality of life.

$$ Check price
144.87144.87Amazon 4.5 logo(1,890+ reviews)

More Back Problems resources