Softpanorama

May the source be with you, but remember the KISS principle ;-)
Contents Bulletin Scripting in shell and Perl Network troubleshooting History Humor

locate command

News See also Tutorial Recommended Links Man pages Reference
Unix Find Tutorial/Find logical expressions Finding SUID/SGUID files Selecting files using their age Traversal control Using find for backups Usage with cpio
Usage with grep Usage with xarg Find and backup Examples Humor Etc

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 makes 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

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
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.

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.


Heelo
Top Visited
Switchboard
Latest
Past week
Past month


NEWS CONTENTS

Old News ;-)

[Jul 16, 2017] 10 Useful 'locate' Command

Jul 16, 2017 | www.tecmint.com
Limit Search Queries to a Specific Number

You can limit your search returns to a required number to avoid redundancy with your search results using the -n command.

For example, if you want just 20 results from your queries, you can type the following command:

$ locate "*.html" -n 20

/home/tecmint/.config/google-chrome/Default/Extensions/aapocclcgogkmnckokdopfmhonfmgoek/0.9_0/main.html
/home/tecmint/.config/google-chrome/Default/Extensions/aohghmighlieiainnegkcijnfilokake/0.9_0/main.html
/home/tecmint/.config/google-chrome/Default/Extensions/felcaaldnbdncclmgdcncolpebgiejap/1.1_0/main.html
/home/tecmint/.config/google-chrome/Default/Extensions/kbfnbcaeplbcioakkpcpgfkobkghlhen/14.752.848_0/forge.html
/home/tecmint/.config/google-chrome/Default/Extensions/kbfnbcaeplbcioakkpcpgfkobkghlhen/14.752.848_0/src/popup.html
/home/tecmint/.config/google-chrome/Default/Extensions/nlipoenfbbikpbjkfpfillcgkoblgpmj/3.9.16_0/additional-feature.html
/home/tecmint/.config/google-chrome/Default/Extensions/nlipoenfbbikpbjkfpfillcgkoblgpmj/3.9.16_0/background.html
/home/tecmint/.config/google-chrome/Default/Extensions/nlipoenfbbikpbjkfpfillcgkoblgpmj/3.9.16_0/edit.html
/home/tecmint/.config/google-chrome/Default/Extensions/nlipoenfbbikpbjkfpfillcgkoblgpmj/3.9.16_0/help.html
/home/tecmint/.config/google-chrome/Default/Extensions/nlipoenfbbikpbjkfpfillcgkoblgpmj/3.9.16_0/options.html
/home/tecmint/.config/google-chrome/Default/Extensions/nlipoenfbbikpbjkfpfillcgkoblgpmj/3.9.16_0/popup.html
/home/tecmint/.config/google-chrome/Default/Extensions/nlipoenfbbikpbjkfpfillcgkoblgpmj/3.9.16_0/purchase.html
/home/tecmint/.config/google-chrome/Default/Extensions/nlipoenfbbikpbjkfpfillcgkoblgpmj/3.9.16_0/upload.html
/home/tecmint/.config/google-chrome/Default/Extensions/nlipoenfbbikpbjkfpfillcgkoblgpmj/3.9.16_0/oauth2/oauth2.html
/home/tecmint/.config/google-chrome/Default/Extensions/nmmhkkegccagdldgiimedpiccmgmieda/1.0.0.2_0/html/craw_window.html
/home/tecmint/.config/google-chrome/Default/Extensions/pkedcjkdefgpdelpbcmbmeomcjbeemfm/5516.1005.0.3_0/cast_route_details.html
/home/tecmint/.config/google-chrome/Default/Extensions/pkedcjkdefgpdelpbcmbmeomcjbeemfm/5516.1005.0.3_0/feedback.html
/home/tecmint/.config/google-chrome/Default/Extensions/pkedcjkdefgpdelpbcmbmeomcjbeemfm/5516.1005.0.3_0/cast_setup/devices.html
/home/tecmint/.config/google-chrome/Default/Extensions/pkedcjkdefgpdelpbcmbmeomcjbeemfm/5516.1005.0.3_0/cast_setup/index.html
/home/tecmint/.config/google-chrome/Default/Extensions/pkedcjkdefgpdelpbcmbmeomcjbeemfm/5516.1005.0.3_0/cast_setup/offers.html

The results will show the first 20 files that end with .html .

3. Display The Number of Matching Entries

If you want to display the count of all matching entries of file " tecmint ", use the locate -c command.

$ locate -c [tecmint]*

1550
4. Ignore Case Sensitive Locate Outputs

By default, locate is configured to process queries in a case sensitive manner meaning TEXT.TXT will point you to a different result than text.txt .

To have locate command ignore case sensitivity and show results for both uppercase and lowercase queries, input commands with the -i option.

$ locate -i *text.txt*

/home/tecmint/TEXT.txt
/home/tecmint/text.txt
5. Refresh mlocate Database

Since locate command relies on a database called mlocate . The said database needs to be updated regularly for the command utility to work
efficiently.

To update the mlocate database, you use a utility called updatedb . It should be noted that you will need superuser privileges for this to work properly, is it needs to be executed as root or sudo privileges.

$ sudo updatedb
6. Display Only Files Present in Your System

When you have an updated mlocate database**, locate command still produces results of files whose physical copies are deleted from your system.

To avoid seeing results of files not present in your machine at the time of punching in the command, you will need to use the locate-e command. The process searches your system to verify the existence of the file you're looking for even if it is still present in your mlocate.db .

$ locate -i -e *text.txt*

/home/tecmint/text.txt
7. Separate Output Entries Without New Line

locate command's default separator is the newline (\\n) character. But if you prefer to use a different separator like the ASCII NUL , you can do so using the -0 command line option.

$ locate -i -0 *text.txt*

/home/tecmint/TEXT.txt/home/tecmint/text.txt
8. Review Your Locate Database

If you're in doubt as to the current status of your mlocate.db , you can easily view the locate database statistics by using the -S command.

$ locate -S

Database /var/lib/mlocate/mlocate.db:
32,246 directories
4,18,850 files
2,92,36,692 bytes in file names
1,13,64,319 bytes used to store database
9. Suppress Error Messages in Locate

Constantly trying to access your locate database does sometimes yield unnecessary error messages stating that you do not have the required privileges to have root access to the mlocate.db , because you're only a normal user and not the required Superuser.

To completely do away with these message, use the -q command.

$ locate "\*.dat" -q*
10. Choose a Different mlocate Location

If you're inputting queries looking for results not present in the default mlocate database and want answers from a different mlocate.db located somewhere else in your system, you can point the locate command to a different mlocate database at a different part of your system with the -d command.

$ locate -d <new db path> <filename>

locate command might seem like one of those utilities that does everything you asked it to do without much of a hustle but in truth, in order for the process to keep its efficiency, the mlocate.db needs to be fed with information every now and then. Failure to do so might render the program a bit useless.

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

Softpanorama hot topic of the month

Softpanorama Recommended



Etc

FAIR USE NOTICE This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available in our efforts to advance understanding of environmental, political, human rights, economic, democracy, scientific, and social justice issues, etc. We believe this constitutes a 'fair use' of any such copyrighted material as provided for in section 107 of the US Copyright Law. In accordance with Title 17 U.S.C. Section 107, the material on this site is distributed without profit exclusivly for research and educational purposes.   If you wish to use copyrighted material from this site for purposes of your own that go beyond 'fair use', you must obtain permission from the copyright owner. 

ABUSE: IPs or network segments from which we detect a stream of probes might be blocked for no less then 90 days. Multiple types of probes increase this period.  

Society

Groupthink : Two Party System as Polyarchy : Corruption of Regulators : Bureaucracies : Understanding Micromanagers and Control Freaks : Toxic Managers :   Harvard Mafia : Diplomatic Communication : Surviving a Bad Performance Review : Insufficient Retirement Funds as Immanent Problem of Neoliberal Regime : PseudoScience : Who Rules America : Neoliberalism  : The Iron Law of Oligarchy : Libertarian Philosophy

Quotes

War and Peace : Skeptical Finance : John Kenneth Galbraith :Talleyrand : Oscar Wilde : Otto Von Bismarck : Keynes : George Carlin : Skeptics : Propaganda  : SE quotes : Language Design and Programming Quotes : Random IT-related quotesSomerset Maugham : Marcus Aurelius : Kurt Vonnegut : Eric Hoffer : Winston Churchill : Napoleon Bonaparte : Ambrose BierceBernard Shaw : Mark Twain Quotes

Bulletin:

Vol 25, No.12 (December, 2013) Rational Fools vs. Efficient Crooks The efficient markets hypothesis : Political Skeptic Bulletin, 2013 : Unemployment Bulletin, 2010 :  Vol 23, No.10 (October, 2011) An observation about corporate security departments : Slightly Skeptical Euromaydan Chronicles, June 2014 : Greenspan legacy bulletin, 2008 : Vol 25, No.10 (October, 2013) Cryptolocker Trojan (Win32/Crilock.A) : Vol 25, No.08 (August, 2013) Cloud providers as intelligence collection hubs : Financial Humor Bulletin, 2010 : Inequality Bulletin, 2009 : Financial Humor Bulletin, 2008 : Copyleft Problems Bulletin, 2004 : Financial Humor Bulletin, 2011 : Energy Bulletin, 2010 : Malware Protection Bulletin, 2010 : Vol 26, No.1 (January, 2013) Object-Oriented Cult : Political Skeptic Bulletin, 2011 : Vol 23, No.11 (November, 2011) Softpanorama classification of sysadmin horror stories : Vol 25, No.05 (May, 2013) Corporate bullshit as a communication method  : Vol 25, No.06 (June, 2013) A Note on the Relationship of Brooks Law and Conway Law

History:

Fifty glorious years (1950-2000): the triumph of the US computer engineering : Donald Knuth : TAoCP and its Influence of Computer Science : Richard Stallman : Linus Torvalds  : Larry Wall  : John K. Ousterhout : CTSS : Multix OS Unix History : Unix shell history : VI editor : History of pipes concept : Solaris : MS DOSProgramming Languages History : PL/1 : Simula 67 : C : History of GCC developmentScripting Languages : Perl history   : OS History : Mail : DNS : SSH : CPU Instruction Sets : SPARC systems 1987-2006 : Norton Commander : Norton Utilities : Norton Ghost : Frontpage history : Malware Defense History : GNU Screen : OSS early history

Classic books:

The Peter Principle : Parkinson Law : 1984 : The Mythical Man-MonthHow to Solve It by George Polya : The Art of Computer Programming : The Elements of Programming Style : The Unix Hater’s Handbook : The Jargon file : The True Believer : Programming Pearls : The Good Soldier Svejk : The Power Elite

Most popular humor pages:

Manifest of the Softpanorama IT Slacker Society : Ten Commandments of the IT Slackers Society : Computer Humor Collection : BSD Logo Story : The Cuckoo's Egg : IT Slang : C++ Humor : ARE YOU A BBS ADDICT? : The Perl Purity Test : Object oriented programmers of all nations : Financial Humor : Financial Humor Bulletin, 2008 : Financial Humor Bulletin, 2010 : The Most Comprehensive Collection of Editor-related Humor : Programming Language Humor : Goldman Sachs related humor : Greenspan humor : C Humor : Scripting Humor : Real Programmers Humor : Web Humor : GPL-related Humor : OFM Humor : Politically Incorrect Humor : IDS Humor : "Linux Sucks" Humor : Russian Musical Humor : Best Russian Programmer Humor : Microsoft plans to buy Catholic Church : Richard Stallman Related Humor : Admin Humor : Perl-related Humor : Linus Torvalds Related humor : PseudoScience Related Humor : Networking Humor : Shell Humor : Financial Humor Bulletin, 2011 : Financial Humor Bulletin, 2012 : Financial Humor Bulletin, 2013 : Java Humor : Software Engineering Humor : Sun Solaris Related Humor : Education Humor : IBM Humor : Assembler-related Humor : VIM Humor : Computer Viruses Humor : Bright tomorrow is rescheduled to a day after tomorrow : Classic Computer Humor

The Last but not Least


Copyright © 1996-2016 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. This document is an industrial compilation designed and created exclusively for educational use and is distributed under the Softpanorama Content License.

The site uses AdSense so you need to be aware of Google privacy policy. You you do not want to be tracked by Google please disable Javascript for this site. This site is perfectly usable without Javascript.

Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

FAIR USE NOTICE This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available to advance understanding of computer science, IT technology, economic, scientific, and social issues. We believe this constitutes a 'fair use' of any such copyrighted material as provided by section 107 of the US Copyright Law according to which such material can be distributed without profit exclusively for research and educational purposes.

This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Grammar and spelling errors should be expected. The site contain some broken links as it develops like a living tree...

You can use PayPal to make a contribution, supporting development of this site and speed up access. In case softpanorama.org is down you can use the at softpanorama.info

Disclaimer:

The statements, views and opinions presented on this web page are those of the author (or referenced source) 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: July 16, 2017