Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
up vote 1 down vote accepted

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.

share|improve this answer

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.
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.