|
Softpanorama |
May the source be with you, but remember the KISS principle ;-)
Softpanorama Search
|
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
To search for files by name without having to actually scan the
directories on the disk (which can be slow), you can use the
locate program. For each shell pattern you give it, locate
searches one or more databases of file names and displays the file names
that contain the pattern. See Shell
Pattern Matching, for details about shell patterns.
If a pattern is a plain string—it contains no metacharacters—locate
displays all file names in the database that contain that string. If a
pattern contains metacharacters, locate only displays file
names that match the pattern exactly. As a result, patterns that contain
metacharacters should usually begin with a ‘*’,
and will most often end with one as well. The exceptions are patterns
that are intended to explicitly match the beginning or end of a file
name.
If you only want locate to match against the last
component of the file names (the “base name” of the files) you can use
the ‘--basename’ option. The
opposite behaviour is the default, but can be selected explicitly by
using the option ‘--wholename’.
The command
locate pattern
is almost equivalent to
find directories -name pattern
where directories are the directories for which the file
name databases contain information. The differences are that the
locate information might be out of date, and that locate
handles wildcards in the pattern slightly differently than find
(see Shell Pattern Matching).
The file name databases contain lists of files that were on the system when the databases were last updated. The system administrator can choose the file name of the default database, the frequency with which the databases are updated, and the directories for which they contain entries.
Here is how to select which file name databases locate
searches. The default is system-dependent. At the time this document was
generated, the default was
/usr/local/var/locatedb.
--database=path
-d path
LOCATE_PATH to set the list of database files to
search. The option overrides the environment variable if both are
used.
GNU locate can read file name databases generated by the
slocate package. However, these generally contain a list of
all the files on the system, and so when using this database,
locate will produce output only for files which are accessible to
you. See Invoking locate, for a
description of the ‘--existing’
option which is used to do this.
The updatedb program can also generate database in a
format compatible with slocate. See
Invoking updatedb, for a description of
its ‘--dbformat’ and ‘--output’
options.
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
locateinstead offind. Orslocateit, 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
slocaterequest has permission to access.The format of the
locate/slocateis simple:locate options pattern.If you're only interested in how many times the pattern is found, you can specify the
-coption in your search, like this:locate -c monoTo search case insensitive use the
-ioption:locate -i whereamiSince 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
updatedbas 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 thelocate -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.
slocate [-qi] [-d
slocate [-i] [-r
slocate [-qv] [-o
slocate [-e
slocate [-Vh] [--version] [--help]
DESCRIPTION
This manual page documents the GNU version of slocate. slocate Enables
system users to search entire filesystems without displaying unauthorized files.
OPTIONS
locate projectLists all files that contain the string "project". If that command does not work you will need to run the command:
slocate -uThis command builds the slocate database which will allow you to use the locate (slocate) command. It may take a few minutes to run.
Finding Files - Table of Contents
Copyright © 1996-2009 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). Site uses AdSense so you need to be aware of Google privacy policy. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.
Disclaimer:
Last modified: August 25, 2009