Searching for a File with a Specific String in the File.
If you're new here, you may want to subscribe to my RSS feed.
To accomplish this I am going to show you a new command and then use | and grep with it. The command is locate. Your computer has a database that has the path names and files that are publicly accessible on your computer. Locate searches that database for all the pathnames and finds the one you are looking for. It is probably the easiest command to remember and use. You just type locate then what you are looking for.
Example:
locate report
You can also search for all files with a certain extension.
Example:
locate '*.jpg'
You may be saying why would I want to do such a specific search? Well lets say you are a web designer that need to modify a template file for all your websites that contain a certain function. Say the function was called capcha and all your template files end in .tpl. You would use you the | command to combine the two function together to accomplish this.
Example:
locate '*.tpl' | grep -R 'capcha' *
This works by running the locate command and then using grep on just the file names that locate returns. Yes you could just type grep -R 'capcha' *, but you would have to be in the root directory of your hard drive and the grep command would search every file on our computer. That would take a long time to complete, and if you are running the search on a web server you could bring it to its knees.
