Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 03-22-2012, 08:12 AM   #1
LQ Newbie
Registered: Oct 2008
Posts: 18

Rep: Reputation: 0
want to use Locate instead of find as it gives some faster results

I used locate command to find files and it was all going well. Then came the need of using some more parameters for finding like finding by size, finding by time & date, etc.

So I used the locate command and its database as follows
sudo find $(locate somefile) -<findoption>
it gave me good results. BUT

everything was messed up when it was about finding files that have spaces in its name.

if filenames have spaces then find command divides the output provided by locate command
Old 03-22-2012, 08:14 AM   #2
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977
just put "'s around the locate command.
Old 03-22-2012, 11:52 AM   #3
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quotes won't work if locate spits out more than a single filename.

Not only that, but the find command is not really correct. The first arguments are really supposed to be a list of top-level directories that find starts the search from, not a list of filenames. It is after all, designed to find files, not evaluate files you already know the location of.

AFAICT, there's really no simple, single command way to take care of this. One solution that would work is to store the output of locate in an array first and use that:

while IFS="" read -d "" -r f; do array+=( "$f" ) ; done < <( locate -0 <pattern> )
find "${array[@]}" -options
This could perhaps be set up as a script or function for ease of use, but the writing of it would be non-trivial, as you'd have to design it to accept the options of two separate programs at once.

But really, if you need to find files by complex criteria, then you should just use find directly, the way it was intended.


find, locate

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Neither grep nor locate find khelper. What does it do? stf92 Slackware 3 06-22-2011 04:29 AM
'locate' utility can't find db imputerate Linux - Newbie 9 05-26-2008 05:52 AM
LXer: Locate, Find, and Whereis LXer Syndicated Linux News 0 03-28-2006 03:06 PM
cannot find a kernel, please locate one bobotoes Linux - Newbie 2 10-07-2004 06:15 PM
Using the results of locate for grep skibud2 Linux - Newbie 1 12-16-2003 01:58 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:37 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration