start page | rating of books | rating of authors | reviews | copyrights

Unix Power ToolsUnix Power ToolsSearch this book

9.18. Using "Fast find" Databases

Berkeley added a handy feature to its find command -- if you give it a single argument, it will search a database for file or directory names that match. For example, if you know there's a file named MH.eps somewhere on the computer but you don't know where, type the following:

% find MH.eps
/nutshell/graphics/cover/MH.eps

That syntax can be confusing to new users: you have to give find just one argument. With more arguments, find searches the filesystem directly. Maybe that's one reason that GNU has a "fast find" utility named locate -- and its find utility always searches, as described in the rest of this chapter. The GNU slocate command is a security-enhanced version of locate. In the rest of this article, I'll describe locate -- but find with a single argument (as shown previously) works about the same way.

The "fast find" database is usually rebuilt every night. So, it's not completely up-to-date, but it's usually close enough. If your system administrator has set this up, the database usually lists all files on the filesystem -- although it may not list files in directories that don't have world-access permission. If the database isn't set up at all, you'll get an error like /usr/lib/find/find.codes: No such file or directory. (If that's the case, you can set up a "fast find" database yourself. Set up your own private locate database, or see Section 9.20.)

Unless you use wildcards, locate does a simple string search, like fgrep, through a list of absolute pathnames. Here's an extreme example:

% locate bin
/bin
/bin/ar
   ...
/home/robin
/home/robin/afile
/home/sally/bin
   ...

You can cut down this output by piping it through grep, sed, and so on. But locate and "fast find" also can use wildcards to limit searches. Section 9.19 explains this in more detail.

locate has an advantage over the "fast find" command: you can have multiple file databases and you can search some or all of them. locate and slocate come with a database-building program.

Because locate is so fast, it's worth trying to use whenever you can. Pipe the output to xargs and any other Unix command, or run a shell or Perl script to test its output -- almost anything will be faster than running a standard find. For example, if you want a long listing of the files, here are two locate commands to do it:

% ls -l `locate whatever`
% locate whatever | xargs ls -ld

There's one problem with that trick. The locate list may be built by root, which can see all the files on the filesystem; your ls -l command may not be able to access all files in the list. But slocate can be configured not to show you files you don't have permission to see.

NOTE: The locate database may need to be updated on your machine before you can use locate, if it's not already in the system's normal cron scripts. Use locate.updatedb to do this, and consider having it run weekly or so if you're going to use locate regularly.

-- JP



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.