Let’s say we are looking for the definition of a function called wp_content_filterize, and we don’t know where to start. Let’s try using grep…
grep -r -i -n 'function wp_content_filterize' *We should see the output right on the console
functions.php:18:function wp_content_filterize($thefilter) {Now we know that the function definition is found in the file functions.php, on line 18.
Grep and punctuation marks
I've often tried to use grep to filter search results for locate and several times I've had to search for a string with locate:
locate svnThis search would obviously include all the '.svn' folders too. Trying to get rid with them withlocate svn | grep -v '.svn'does not work, since grep uses basic regular expressions, and the period mark will simply match any character.To explicitly indicate the usage of a punctuation mark, one has to put it in brackets. The statement will now look something like:
locate svn | grep -v '[.]svn'I just scanned through the grep man pages, and everything is explained pretty clearly there. I'd recommend reading the bit on regular expressions.
Grep Recursively Through Single File Extension
You can use the following grep command to search through a directory, recursively, but only looking at a specific file pattern.
Here's another example with the -r, -i and -o options all added.
grep -r --include=<pattern> <string> <directory>Here is an example that searches recursively starting at /home/joel/ and including only php files.
grep -r --include=*.php "public auction" ./Other options you might want to add to the command:
| -i | Case insensitive match. |
| -c | Count of matches; suppressing normal output. One line per file match. |
| -o | Show only the part of a matching line that matches. |
grep -rio --include=*.php "public auction" ./