Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zhangchenchen/7d0d9465c4b84c54170f2bec9191fe97 to your computer and use it in GitHub Desktop.
Save zhangchenchen/7d0d9465c4b84c54170f2bec9191fe97 to your computer and use it in GitHub Desktop.
find all files containing specific text on Linux
grep -rnw '/path/to/somewhere/' -e "pattern"
-r or -R is recursive,
-n is line number, and
-w stands match the whole word.
-l (lower-case L) can be added to just give the file name of matching files.
Along with these, --exclude or --include parameter could be used for efficient searching. Something like below:
grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
This will only search through the files which have .c or .h extensions.
grep -rnw '/path/to/somewhere/' -e "pattern" | wc -l #get the num of files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment