This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new!


CLI Magic: locate, slocate, and rlocate

By Joe Barr on June 27, 2005 (8:00:00 AM)

Share    Print    Comments   

Sometimes you need to find a file right now, and you don't want to search for it one directory after another. That's the time to visit the CLI and locate the missing file. Or slocate it, depending on your distribution. There is just one problem with using locate or slocate, and that's staying up to date. Here's how they work and how to use them, and a brief tease on rlocate,their nimble, more timely, heir apparent.
Slocate and locate both do essentially the same thing: search a database containing the file names and locations on the system for a match and report all that are found. Both count on another program -- updatedb -- to do the heavy lifting by creating/maintaining the database to be searched. Slocate provides greater security by storing the permissions and ownership of each file, and then only showing the files that the user running the slocate request has permission to access.

The format of the locate/slocate is simple: locate options pattern. It's a good idea to be as specific as possible with the search pattern, because locate will list every file name it finds that contains a match. If I use locate to search my SUSE 9.3 desktop system for mono, like this:

locate mono

It responds with over 400 lines of output. But if I search for libmono instead of simply mono, locate returns only 3 matches.

If you're only interested in how many times the pattern is found, you can specify the -c option in your search, like this:

locate -c mono

File names in Linux are -- sometimes infuriatingly so users who are new to Unix -- case sensitive. Trying to remember if a file name is whereami, WhereamI, or WhereAmI before searching for it can add frustration to the task. Tell locate that you are an insensitive lout who is totally case insensitive by adding the -i option to the search, like this:

locate -i whereami

Updating the database

Since updatedb normally runs just once a day, sometimes you need to find a file that has been created since the last update. When that's the case, just enter the command updatedb as root and let it run. It may take several minutes to complete, or even longer if you have a large number of files to be accounted for. To find out how large your database is, enter the locate -S, like this:

warthawg@linux:~> locate -S
Database<nobr> <wbr></nobr>/var/lib/locatedb is in the LOCATE02 format.
Locate database size: 3411612 bytes
Filenames: 401444 with a cumulative length of 20196439 bytes
        of which 38656 contain whitespace,
        0 contain newline characters,
        and 43 contain characters with the high bit set.
Compression ratio 83.11%

Bah, that's too klunky!

Having to run the database update program before doing a search in order to have access to the latest files on your system is far from being an elegant solution. Some people just don't want to wait. If that describes you, then you might want to check out a new project called rlocate by Rasto Levrinc. It's based on slocate but with nearly real-time search capabilities.

rlocate -- currently available in a beta release -- requires a Linux kernel at 2.6 or later. It functions as a kernel module which maintain a daily database containing the files and directories created since the last time updatedb was run.

When slocate is executed, both the daily database of new files maintained by the rlocate kernel module and the nightly database of all files are searched. The result is a search that yields results no more than 2 seconds old.

We'll keep an eye on this promising new tool and bring a complete report at a later date. Until then, make use of locate/slocate and updatedb -- and their respective man pages -- to find those files you need, when you need them.

Share    Print    Comments   


on CLI Magic: locate, slocate, and rlocate

Note: Comments are owned by the poster. We are not responsible for their content.

What distro?

Posted by: Anonymous Coward on June 27, 2005 09:34 PM
Not a bad article.. nice little review of the (s)locate command and its basic functionality. I'm curious to know what distro of Linux your running?

The reason I ask is that on my Slackware and Gentoo-based systems, "locate -S" doesn't work period.

Can I assume this is Redhat? SuSE?


Re:What distro?

Posted by: Anonymous Coward on June 28, 2005 04:12 AM
I'm running <a href="" title=""></a>, which is just RH Enterprise 4, compiled from source, and the -S option is not there. going to try <a href="" title=""></a> Core 3 next, but i'm sure that it won't be there either.


Re:What distro?

Posted by: Anonymous Coward on June 28, 2005 05:51 AM
updatedb will update slocate database


Re:What distro?

Posted by: Joe Barr on June 28, 2005 06:19 AM
Try it as root and see if that makes a difference. It does in SuSE 9.3, which is the distro I am currently running on my desktop.


locate is your friend

Posted by: Anonymous Coward on June 29, 2005 01:28 AM
I like to use locate and grep together so that I can
find that elusive program that I can't quite remember that actual name of.
For example, if I am looking for a program that had "conf" in it I would type:

locate conf | grep bin/

That would show me all of the files that have "conf" in their names that are in the<nobr> <wbr></nobr>/bin<nobr> <wbr></nobr>/sbin<nobr> <wbr></nobr>/usr/bin<nobr> <wbr></nobr>/usr/sbin and<nobr> <wbr></nobr>/usr/X11R6/bin directories.

Debian based users should try it with "apt" and it
will show apt-cache; another nice program to become familiar with.



The `find` command really ought to be mentioned.

Posted by: Anonymous Coward on July 01, 2005 04:26 AM
That's a nice article. But it's really incomplete without mentioning the `find` command. Sometimes you want to use locate, and sometimes you don't. Knowing what tools are at your disposal, and when to use them (and when not to!) is what really makes or breaks a technical expert (regardless of what field they are in).

Thanks for this addition to the CLI series.


slocate store permissions and ownership

Posted by: Anonymous Coward on February 10, 2007 12:04 PM
>>Slocate provides greater security by storing the
>>permissions and ownership of each file

as i read about slocate in man pages, it doesn't store it in the database, where they should be??


This story has been archived. Comments can no longer be posted.

Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya