3

How can I locate a file using locate in CentOS under a specific directory from terminal? Locate search the whole database!

2 Answers 2

4

locate does not seem to have an option to do this, but you can still search specific directories using one of the following:

  • Use wildcards in your search. Example: locate '*/directory/*filename*'
  • Use grep with locate. Example: locate filename | grep /directory/
  • Use the find command. Example: find /my/directory/ -name filename. You can also restrict your search to directories or files by appending -type d or -type f. To find a file named 'myScript' in your home folder you could do this: find ~/ -name myScript -type f. This will search for a file (not directories) named exactly 'myScript' inside your home folder.
1

There is no option for that functionality in the output from man locate on CentOS 6.5, at least. But, you could get pseudo-functionality by changing a search term. For example, locate cron might produce too much output, but locate '/var/log/cron' would limit the results to those items in the locate database that match the search terms. Or, a pipe would work: locate cron | grep '/var/log/' Otherwise, use find: find /path/to/search -name '*cron*' or similar.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.