Softpanorama
(slightly skeptical) Open Source Software Educational Society

May the source be with you, but remember the KISS principle ;-)

Google   


Unix locate command

On Solaris it needs to be installed from GNU findutils. For linux alternative implementation is rlocate. There is also a secure version slocate - Security Enhanced version of the GNU Locate

locate [-d path | --database=path] [-e | --existing] [-i | --ignore-case ] [--version] [--help] pattern...

Locate provides a secure way to index and quickly search for files on your system. It uses index database to sped search but that means that it dependent of the currency of the database. This is a deficiency: you buy speed at the expense of currency. The index database to make searching much faster then find. \

-d path
--database=path
Instead of searching the default file name database, search the file name databases in path, which is a colon-separated list of database file names. You can also use the environment variable LOCATE_PATH to set the list of database files to search. The option overrides the environment variable if both are used.

The file name database format changed starting with GNU find and locate version 4.0 to allow machines with different byte orderings to share the databases. This version of locate can automatically recognize and read databases produced for older versions of GNU locate or Unix versions of locate or find.
 

-e
--existing
Only print out such names that currently exist (instead of such names that existed when the database was created). Note that this may slow down the program a lot, if there are many matches in the database.
 
-i
--ignore-case
Ignore case distinctions in both the pattern and the file names.
--help Print a summary of the options to locate and exit.
--version Print the version number of locate and exit.



Examples:

locate perl -- Locate file perl in the DB and Print the path.

locate -i perl -- Same search as above, case insensitive.

locate -q perl  -- Run in Quiet Mode.

locate -n 2 perl -- Limit the no. of results shown to 2 first.

locate -U locater -o locateDB -- Create index DB starting at locater and store the index file in locateDB.

In the above example the system would locate perl on the local machine.

Note: You may need to run the "updatedb" command to update the database in order to find the file you are searching for. This command should be ran from cron daily or several times a day.

RELATED COMMANDS

find
whereis
xargs

 

Old News ;-)

1995 Summary unix find and fastfind on SunOS and-or Solaris

Sun Managers did it again. Answered all three questions correctly in less
than 24 hours!
 

Kudos to Dr. Peter Watkins (peter@jrc.nl) for answering almost immediatly
with the correct answers. I'm going to use his summary (which is brief)
since I could do no better. Peter writes...
 

>Taking your questions in a completely random order;
>
> + For Solaris 2.x try the GNU version of 'find'. Currently
> at version 3.8 I think. This has the 'locate' function
> which replicates the fastfind feature.
> Try ugle.unit.no:/pub/gnu/find-3.8.tar.gz
 

Quite so. I grabbed a copy and it's a perfect solution for my solaris
machines that never came with a fast find. Gnu's Great!
 

> + I suspect that the reason for duplicate names is that
> updatedb is actually a script (try looking at it) which
> descends the filesystem structure. Consequently if you
> specify / and /fred then fred will get searched twice.
> If you look at the updatedb script you will see that
> certain directories can be included/excluded there. Try
> that instead.
 

Correct again. I was specifically specifying each partition when in
fact the script is smart enough to assume you want everything minus
a few obvious things that no one would want. This was creating
redundancy which accounts for files being reported twice.
 

> + Your problem of updatedb not working at all seems a bit
> odd. I suggest you first try;
> /usr/lib/find/updatedb / /usr /nat1 /nat2
> by hand without chucking the output. Possibly the 'find'
> command is not where updatedb expects - look at the
> updatedb script again.
 

And so I did. It almost immediatly blew up complaining about a lack
of /tmp space. I cheated by changing the script to use a tmp space
out on a data disk. Now it works like a champ. First time in years!
 

My thanks to Dr. Peter Watkins as well as the other 9 respondees who
wrote later. Most were not able to answer all three questions but
everyone had something interesting to share with me. To see what...
(or if you're one of the other 9 respondees and want to see your name
in print)... credits and micro-summaries below...
 

-drp

Linux.com CLI Magic locate, slocate, and rlocate

Sometimes you need to find a file that was present in the filesystem for a long time (for example some unix command). In this case you can use locate instead of find. 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.

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

To search case insensitive use the -i option:

locate -i whereami

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

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.


Reference

slocate - Security Enhanced version of the GNU Locate

slocate [-qi] [-d ] [--database=]

slocate [-i] [-r ] [--regexp=]

slocate [-qv] [-o ] [--output=]

slocate [-e ] [-f ] <[-l ] [-c] <[-U ] [-u]>

slocate [-Vh] [--version] [--help]

 

DESCRIPTION


Secure Locate provides a secure way to index and quickly search for files on your system. It uses incremental encoding just like GNU locate to compress its database to make searching faster, but it will also store file permissions and ownership so that users will not see files they do not have access to.


This manual page documents the GNU version of slocate. slocate Enables system users to search entire filesystems without displaying unauthorized files.

OPTIONS

-u
Create slocate database starting at path /.
-U dir
Create slocate database starting at path dir.
-e
Exclude directories from the slocate database.
-f
Exclude files on specific file systems from the slocate database.
-c
Parse '/etc/updatedb.conf' when updating the slocate database.
-l
Security level. 0 turns security checks off. This will make searchs faster. 1 turns security checks on. This is the default.
-i
Does a case insensitive search.
-q
Quiet mode. Error messages are suppressed.
-n
Limit the amount of results shown to .
-r
--regexp= Search the database using a basic POSIX regular expression.
-o
--output= Specifies the database to create.
-d
--database= Specifies the path of databases to search in.
-h
--help Display this help.
-v
--verbose Verbose mode. Display files when creating database.
-V
--version Display version.

 

EXAMPLES

locate project
Lists all files that contain the string "project". If that command does not work you will need to run the command:
 
slocate -u
This command builds the slocate database which will allow you to use the locate (slocate) command. It may take a few minutes to run.
 

Recommended Links


In case of broken links please try to use Google search. If you find the page please notify us about new location
Google     

SourceForge.net rlocate

Finding Files - Table of Contents


Copyright © 1996-2008 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

Standard disclaimer: The statements, views and opinions presented on this web page are those of the author and are not endorsed by, nor do they necessarily reflect, the opinions of the author present and former employers, SDNP or any other organization the author may be associated with. We do not warrant the correctness of the information provided or its fitness for any purpose.

Last modified: September 15, 2008