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:
- grep - this is the command itself
- --include=*.{c,h} - search the listed file extensions
- -rnw 'path/to/somewhere/' - search recursively, starting at the provided path
- -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()"
Cool, huh?!