Grep For Text In All Files In A Directory

16th January 2013 - 3 minutes read time

Searching all files in a directory and sub-directories for a particular term is really useful and comes in handy in all sorts of situations. It is available on all Linux systems and the basic syntax is as follows.

grep -r -i pattern directory

The -r flag is used to recursively search underneath the given directory and the -i flag is used to ignore case. The pattern is a normal regular expression, which can be changed to an extended set by using the -E flag.

An example of finding a search term in everything under the current directory would be like this.

grep -r -i searchterm ./

Remember that if you want to use regular expression characters then you'll need to escape them. For example, to find all PHP opening tags you will need to escape the < and ? characters.

grep -r -i \<\?php ./

Add new comment

The content of this field is kept private and will not be shown publicly.