我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
当前回答
重复和案例不敏感的捕捉与线号:
grep -inr "Text" folder/to/be/searched/
其他回答
如果您的捕捉不支持重复搜索,您可以将搜索与 xargs 相结合:
find / -type f | xargs grep 'text-to-find-here'
我觉得这比找到 -exec 的格式更容易记住。
这将产生相匹配线的文件名和内容,例如。
/home/rob/file:text-to-find-here
您可能想添加到捕捉的选项旗帜:
-i - 案例不敏感搜索 -l - 只输出相匹配的行输出(而不是相匹配的行输出)
这个捕捉命令会给你一个准确的结果,当你正在寻找特定的文本在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"
正如彼得在以前的回答中提到的那样,所有以前的回答都建议抓住并找到。
但有一个更复杂的方式使用Gnome Commander与完美的GUI和数以百万计的选项自2001年以来,并找到文件只是其中之一。
试试这:
find . | xargs grep 'word' -sl
您可以使用 ack. 它就像输入源代码. 您可以扫描您的整个文件系统。
只是做:
ack 'text-to-find-here'
在你的根目录中。
您也可以使用常规表达式,指定文件类型等。
更新
我刚刚发现The Silver Searcher,这类似于Ack,但比它快3至5倍,甚至忽略了来自.gitignore 文件的模式。