Recursive Text Search with Grep and File Patterns

command-line-interfacegrepsearch

Given this example folder structure:

/folder1/file1.txt
/folder1/file2.djd
/folder2/file3.txt
/folder2/file2.fha

How do I do a recursive text search on all *.txt files with grep from "/"?

("grep -r <pattern> *.txt" fails when run from "/", since there are no .txt files in that folder.)

Best Answer

My version of GNU Grep has a switch for this:

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

Described as follows:

--include=GLOB

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

Related Topic