Skip to main content

find all files containing specific text / pattern

List the files containing pattern

grep -ril ['PATH'] -e 'PATTERN'

in current path:

grep -ril ./ -e 'PATTERN'

count them only:

grep -ril ./ -e 'PATTERN' | wc -l

where

  • -r recursive lazy - read all files under each directory, recursively, following symbolic links only if they are on the command line. Note that if no file operand is given, grep searches the working directory. This is equivalent to the -d recurse option.

  • -l (lower-case L) show the file name, not the result itself

more options

  • -n show line number

  • -R recursive greedy - read all files under each directory, recursively. Follow all symbolic links, unlike -r.

  • -i stands for ignore case

  • -w stands for match the whole word

  • -o print only the matched (non-empty) parts of a matching line, with each such part on a separate output line

  • --color=auto mark up the matching text

  • --include Example: --include=\*.{c,h} will only search through those files which have .c or .h extensions

  • --exclude Example: --exclude=*.o will exclude searching all the files ending with .o extension

  • --exclude-dir Example: --exclude-dir={dir1,dir2,*.dst} will exclude the dirs dir1/, dir2/ and all of them matching *.dst/

PATTERN is normally case sensitiv
man page: http://man7.org/linux/man-pages/man1/grep.1.html