Recursive text search with grep and file patterns

Solution 1:

My version of GNU Grep has a switch for this:

grep -R --include='*.txt' $Pattern

Described as follows:


Search only files whose base name matches GLOB (using wildcard matching as described under --exclude).

Solution 2:

If you have a large number of files it would be useful to incorporate xargs into the command to avoid an 'Argument list too long' error.

find . -name '*.txt' -print | xargs grep <pattern>

Solution 3:

you might be able to make use of your zsh's EXTENDED_GLOB option (docs)

grep <pattern> **/*.txt