Search a Database of Filenames


locate

Know the name of a file, or even part of the name, but don't know where it resides on your system? That's what locate is for. The locate command looks for files, programs, and directories matching your search term. Any matching results are printed to your terminal, one after the other.

Note

In order to save space, I've replaced the first part of the path - /home/scott - with an ellipses.


$ locate haggard .../txt/rider_haggard .../txt/rider_haggard/Queen_of_the_Dawn.txt .../txt/rider_haggard/Allan_and_the_Ice-Gods.txt .../txt/rider_haggard/Heu-Heu_or_The_Monster.txt 


Your search results show up quickly because locate isn't searching your system in real time. Instead, locate searches a database of filenames that is automatically updated daily (more about that in "Update the Database Used by locate" later in this chapter). Because locate searches a precreated database, its results appear almost instantaneously.

On your computer, though, you're probably using slocate instead of locateyou're just not aware of it. The slocate command (which stands for secure locate) is a more recent version that won't search directories that the user running slocate doesn't have permission to view (for instance, if you're not root, results from /root shouldn't show up when you search with locate). Before slocate, locate would spit back many errors complaining about permission problems; with slocate, those errors are a thing of the past.

To verify how slocate works, try the following. Note that the first search, done when you're a normal user and not root, fails. Use su to become root, run locate again, and bingo! Search results appear (slocate.db is the database file used by slocate, by the way).

$ locate slocate.db $ su - # locate slocate.db /var/lib/slocate/slocate.db.tmp /var/lib/slocate/slocate.db 


To make things easy on users, however, most systems create a soft link for /usr/bin/locate that points to /usr/bin/slocate. To verify that your Linux distribution does this, try the following (these particular results are from a box running K/Ubuntu 5.10, a Debian-based distribution, and I've removed some data so I can focus on the important information):

$ ls -l /usr/bin/locate root root /usr/bin/locate -> slocate 


Because it's transparent to the user that she's running slocate, and because locate uses fewer letters and is therefore quicker to type, we're going to refer to locate in this book, even though slocate is the actual command that's being run.



Linux Phrasebook
Linux Phrasebook
ISBN: 0672328380
EAN: 2147483647
Year: 2007
Pages: 288

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net