How do I find specific text in a folder ?

grep -rnw '/path/to/somewhere/' -e 'pattern'
grep -rnw 'pattern' '/path/to/somewhere/'
  • -r or -R 递归,
  • -n 行号
  • -w 全部匹配
  • -l (小写 L) 只是列出文件名

此外,还可以使用 –exclude, –include,–exclude-dir标志进行高效搜索:

// This will only search through those files which have .c or .h extensions:
grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"

// This will exclude searching all the files ending with .o extension:
grep --exclude=*.o -rnw '/path/to/somewhere/' -e "pattern"

// For directories it's possible to exclude a particular directory(ies) through --exclude-dir parameter. For example, this will exclude the dirs dir1/, dir2/ and all of them matching *.dst/:
grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern"