How do I use the locate command within a specified directory?

来源:互联网 发布:java中的接口的作用 编辑:程序博客网 时间:2024/05/16 09:52

From:  http://serverfault.com/questions/313733/how-do-i-use-the-locate-command-within-a-specified-directory 

I am using the locate command on Linux. My current usage of it searches through the entire filesystem. I only want it to search within a specific directory. How can I do this?

share|improve this question
 add comment

3 Answers

activeoldestvotes
up vote4down vote

Another approach would be to use the pattern matching in locate:

locate '/some/directory/*filename*'

Compare the output of the commands below:

$ locate tmpfile/usr/lib64/perl5/auto/POSIX/tmpfile.al/usr/share/man/fr/man3/tmpfile.3.gz/usr/share/man/ja/man3/tmpfile.3.gz/usr/share/man/man3/tmpfile.3.gz/usr/share/man/man3p/tmpfile.3p.gz$$ locate '/usr/lib64/*tmpfile*'/usr/lib64/perl5/auto/POSIX/tmpfile.al$ 

locate /usr/lib*tmpfile* gives the same result.

share|improve this answer
 
3 
+1 This is the correct answer. Note that if you include the wildcard, the boundaries become fixed. That is in the above example, $ locate '/usr/lib64/*tmpfile' will return no results. –  xofer Sep 21 '11 at 4:36
add comment
up vote2down vote

The simplest answer I can think of is

locate search_term | grep "/specified/directory"

The better solution might be to use "find" and its path options...

share|improve this answer
 add comment
up vote2down vote

Create slocate database for your specific directory with:

locate -U /path/to/directory

and search with:

locate <search_string>

UPDATE

This works fine on my Gentoo system but CentOS doesn't include -U option. So, you can try below instead:

Build the database with:

# updatedb -U /path/to/dir -o dir_locate.db

and search:

# locate -d dir_locate.db <search_string>
share|improve this answer
 
1 
"locate -U" throws an invalid option error... Guessing you meant "updatedb -U"? Also good to note this requires an mlocate.db rebuild (updatedb) if you ever want to search another directory. –  gharper Sep 21 '11 at 2:59
 
Oh, on my Gentoo it works fine but CentOS doesn't include this option. –  quanta Sep 21 '11 at 3:21
 
Down vote? Could you please leave a comment? –  quanta Sep 21 '11 at 4:44
add comment
0 0
原创粉丝点击