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.
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.
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
Once we have our database set up, we can use the
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.
Here are a few options that you may find helpful.
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
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
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