LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to use locate command (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-use-locate-command-190688/)

MartinOG 06-07-2004 10:23 AM

How to use locate command
 
How do I search for a file with locate (slocate actually)? My problem is if I want to search for a file with a small name like ld, I get every file that has 'ld' somewhere in the name. How can I say 'I want the file that is named *exactly* ld'? Does locate not handle this? I know about the find command, but locate seems much simpler if I could just get it to work. Thanks.

jim mcnamara 06-07-2004 10:36 AM

You will have to use regular expressions that return fewer hits.
Code:

[^ld]
will find all files that start with ld for example.

locate and slocate are really meant to find files based on parts of the name,
find is a better choice when you know the whole name.

unix tools usually do one thing very well. So, if you are using a tool that doesn't do the job, you are probably using the wrong tool.

MartinOG 06-07-2004 01:19 PM

Thanks Jim. Actually, I couldn't get locate [^ld] to work (or locate -r [^ld]). But I guess I'll stick to find, although I wonder if find will be as fast...let me check...no, find / -name test123 -print is a lot slower than locate test123. Wait a minute... now locate test123 doesn't even work...

How does locate work exactly? I've read the man page, and there's something about creating the database. Am I supposed to create a database before using it? But it was working before..maybe I corrupted it? I created a new file, test123, then did locate test123 and it didn't find it, then did ls test123 and its there.

Anyone know what I'm doing wrong here? I could just stick to using find, but when locate was working for me it seemed to be pretty fast so I'd like to use locate. Thanks.

kevinalm 06-07-2004 01:59 PM

Every so often you have to run (as root) updatedb. Some systems do this automatically sceduling via cron or anacron. This creates the locate database, essentially an index of all the files on the mounted filesystems. locate searches this database, not the actual filesystem for a match. This is much faster than searching the filesystem but may be inaccurate since there may changes since the last updatedb. Also, updatedb takes a while. IIRC find searches the actual filesystem and is slower than locate.


All times are GMT -5. The time now is 02:15 PM.