Linux – How to “grep” recursively filtering the name of the files I want with wildcards

bashcommand linefile-filtergreplinux

When I want to perform a recursive grep search in the current directory, I usually do:

grep -ir "string" .

But that command searches inside all kinds of files, including binary files (pictures, audio, video, etc…) which results in a very slow search process.

If I do this, for example, it doesn't work:

grep -ir "string" *.php

It doesn't work because there are no PHP files inside the current directory, but inside some of the subdirectories in the current directory, and the subdirectories' names don't end with ".php" so grep doesn't look inside them.

So, how can I do a recursive search from the current directory but also specifying filename wildcards? (i.e: only search in files which end in a specific extension)

Best Answer

Use grep's --include option:

grep -ir "string" --include="*.php" .
Related Question