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
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
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