3

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

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
MD. Mohiuddin Ahmed
  • 665
  • 3
  • 11
  • 20

2 Answers2

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.
arnefm
  • 3,132
  • 18
  • 16
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.

Christopher
  • 15,611
  • 7
  • 51
  • 64