Find files containing text in Linux

One of the more frustrating experiences that happens to me (and maybe you too!) while working on my computer is trying to find a specific file, or a file containing a phrase or programming function, and it can be hard to figure out how to move forward.

Fortunately, there's a super cool Linux terminal command, grep, that can come to the rescue.

grep --include=*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"

Let's break that down:

  1. grep - this is the command itself
  2. --include=*.{c,h} - search the listed file extensions
  3. -rnw 'path/to/somewhere/' - search recursively, starting at the provided path
  4. -e "pattern" - this is the text or word to locate

Here's an actual example:

grep --include=*.{java,md} -rnw '/home/greg/repos/' -e "formLogin()"

A portion of the results

Cool, huh?!