我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
当前回答
这个捕捉命令会给你一个准确的结果,当你正在寻找特定的文本在Linux -
grep -inRsH “文本要被搜索” /path/to/dir(它可以是“。
i stands for ignore case distinctions R stands for recursive and it also includes symlinks. It is better to use 'R' instead of 'r' n stands for "it will print line number." s stands for "suppress error messages" H stands for "it will print the file name for each match"
其他回答
您可以使用以下命令从文件中找到特定文本:
cat file | grep 'abc' | cut -d':' -f2
做下列事:
grep -rnw '/path/to/somewhere/' -e 'pattern'
-r 或 -R 是重复的, -n 是字符号, -w 是相匹配整个单词的。 -l (较低的 L 案例) 可以添加到只提供相匹配文件的文件名。
与此同时, - 排除, - 包括, - 排除-dir 旗帜可以用于有效的搜索:
这只会通过具有.c 或.h 扩展的文件进行搜索: grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern" 这将排除与.o 扩展结束的所有文件进行搜索: grep --exclude=\*.o -rnw '/path/to/somewhere/' -e "pattern" 对于目录,可以使用 --exclude-dir 参数排除一个或多个目录。
这对我来说非常好,为了实现几乎与你的同样的目的。
更多选项,看看男人抓住。
使用 grep -ilR:
grep -Ril "text-to-find-here" /
i 代表忽略案例(可选在您的案例中)。R 代表重复。l 代表“显示文件名,而不是结果本身”。 / 代表在您的机器的根源开始。
使用 pwd 从您所在的任何目录中搜索,向下滑动
grep -rnw `pwd` -e "pattern"
取决于您正在使用的 grep 版本,您可以忽略 pwd. 在更新的版本中,如果没有目录,则似乎是 grep 的默认情况。
这样:
格雷普 -rnw -e “模式”
或
格雷普 -rnw “模式”
他们会像上面那样做!
我很感动如何简单的吸引力使它与“rl”:
grep -rl 'pattern_to_find' /path/where/to/find
-r to recursively find a file / directory inside directories..
-l to list files matching the 'pattern'
使用“r”而不“l”以查看文件名跟随文本中的模式!
grep -r 'pattern_to_find' /path/where/to/find
它只是完美工作......