Skip to content

Instantly share code, notes, and snippets.

@gayanvirajith
Last active August 29, 2015 14:04
Show Gist options
  • Save gayanvirajith/9b9e07f75de6aede4688 to your computer and use it in GitHub Desktop.
Save gayanvirajith/9b9e07f75de6aede4688 to your computer and use it in GitHub Desktop.
Find all files containing a text string on Linux.

grep -Ril "text-to-find-here" "directory path"

  • i stands for upper/lower case (optional in your case).
  • R stands for recursive.
  • l stands for "show the file name, not the result itself`.

grep -rnw 'directory' -e "pattern" # this works for me all the time.

  • r is recursive
  • n is line number
  • w stands match the whole word

grep --include=*.{c,h} -rnw 'directory' -e "pattern"

  • This will only search through the files which have .c or .h extensions. Similarly a sample use of --exclude:

grep --exclude=*.o -rnw 'directory' -e "pattern"

  • Above will exclude searching all the files ending with .o extension.

grep -Ir --exclude=*.{c,h} "pattern" *

  • -I Ignores binary files

Sources

Stackoverflow article

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment