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.

-q
Suppress error messages that might arise from not having permissions.
-n
Specify a number after to limit the number of outputs.
-i
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!

Command Line Kung Fu

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

Command Line Kung Fu is packed with dozens of tips and practical real-world examples. You won't find theoretical examples in this book. The examples demonstrate how to solve actual problems. The tactics are easy to find, too. Each chapter covers a specific topic and groups related tips and examples together.

$ Check price
14.9914.99Amazon 4.5 logo(27+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Inversion Therapy Table

Aching back from coding all day? Try Back Problems

Stretch out your back and relieve your back muscles with inversion therapy. This device counteracts the forces of gravity on the body by decompressing and elongating the spine. By using this product just ten minutes a day, you can be well on your way to improved circulation and posture while relieving muscle aches, back pain and stress.

$$ Check price
119.98119.98Amazon 4.5 logo(1,700+ reviews)

More Back Problems resources

Ad