Grep For Text In All Files In A Directory

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.
CAPTCHA
5 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.