我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
当前回答
试试这:
find . | xargs grep 'word' -sl
其他回答
试试这
find . -type f -name some_file_name.xml -exec grep -H PUT_YOUR_STRING_HERE {} \;
如果您的捕捉不支持重复搜索,您可以将搜索与 xargs 相结合:
find / -type f | xargs grep 'text-to-find-here'
我觉得这比找到 -exec 的格式更容易记住。
这将产生相匹配线的文件名和内容,例如。
/home/rob/file:text-to-find-here
您可能想添加到捕捉的选项旗帜:
-i - 案例不敏感搜索 -l - 只输出相匹配的行输出(而不是相匹配的行输出)
试试:
find / -type f -exec grep -H 'text-to-find-here' {} \;
将搜索所有文件系统,因为 / 是根文件夹。
Home 文件夹使用:
find ~/ -type f -exec grep -H 'text-to-find-here' {} \;
目前使用文件夹:
find ./ -type f -exec grep -H 'text-to-find-here' {} \;
您可以使用 ack. 它就像输入源代码. 您可以扫描您的整个文件系统。
只是做:
ack 'text-to-find-here'
在你的根目录中。
您也可以使用常规表达式,指定文件类型等。
更新
我刚刚发现The Silver Searcher,这类似于Ack,但比它快3至5倍,甚至忽略了来自.gitignore 文件的模式。
您可以使用下面的命令,因为您不想要文件名,但您想要从所有文件中搜索。
grep -e TEXT *.log | cut -d' ' --complement -s -f1
捕捉与 -e 选项是相当快的与其他选项相比,因为它是PATTERN 比赛