How can I locate a file using locate in CentOS under a specific directory from terminal?
Locate search the whole database!
2 Answers
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
grepwithlocate. Example:locate filename | grep /directory/ - Use the
findcommand. Example:find /my/directory/ -name filename. You can also restrict your search to directories or files by appending-type dor-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.
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.