LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   want to use Locate instead of find as it gives some faster results (http://www.linuxquestions.org/questions/linux-newbie-8/want-to-use-locate-instead-of-find-as-it-gives-some-faster-results-935870/)

technocp 03-22-2012 08:12 AM

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

acid_kewpie 03-22-2012 08:14 AM

just put "'s around the locate command.

David the H. 03-22-2012 11:52 AM

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:

Code:

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.


All times are GMT -5. The time now is 12:24 AM.