Use Grep to Recursively Search for Text for a Given File Extension

Published: Aug 22, 2018
Updated: Sep 9, 2022

Ran into a scenario at work where I needed to search for a piece of text, but only in files with a certain extension, and in all subdirectories (and their subdirectories, and so on).

I knew of grep, but had only used it to search for text in a single file. Well, turns out it can do way more than that.

After peeking at the manual (man grep) and consulting the google machine for more examples, here’s the command that did what I needed.

Note: .txt should be replaced with the desired extension.

grep -i -r --include '*.txt' 'some text' .

The breakdown: