我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
当前回答
你的命令是正确的. 你只需要添加 -l 来抓住:
find / -type f -exec grep -l 'text-to-find-here' {} \;
其他回答
find /path -type f -exec grep -l "string" {} \;
评论的解释
查找是一个命令,允许您在特定路径的子目录中找到文件和其他对象,如目录和链接,如果您不指定该文件名称应满足的面具,则列出所有目录对象。
-type f specifies that it should proceed only files, not directories etc.
-exec grep specifies that for every found file, it should run grep command, passing its filename as an argument to it, by replacing {} with the filename
试试:
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' {} \;
如果您的捕捉不支持重复搜索,您可以将搜索与 xargs 相结合:
find / -type f | xargs grep 'text-to-find-here'
我觉得这比找到 -exec 的格式更容易记住。
这将产生相匹配线的文件名和内容,例如。
/home/rob/file:text-to-find-here
您可能想添加到捕捉的选项旗帜:
-i - 案例不敏感搜索 -l - 只输出相匹配的行输出(而不是相匹配的行输出)
我很感动如何简单的吸引力使它与“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
它只是完美工作......
如果使用在您的例子中找到相似的,最好添加 -s ( - 没有消息) 捕捉,并在命令结束时 2>/dev/null 避免大量的许可拒绝的消息由捕捉发送,并找到:
find / -type f -exec grep -sH 'text-to-find-here' {} \; 2>/dev/null
更快、更容易的工具在同一用途中存在 - 请参见下面. 最好尝试它们,只要它们在您的平台上可用,当然:
更快、更容易的替代品
RipGrep - 最快的搜索工具周围:
rg 'text-to-find-here' / -l
ag 'text-to-find-here' / -l
ACK:
ack 'text-to-find-here' / -l
警告:除非你真的不能避免,不要从“/”(根目录)搜索,以避免漫长而不有效的搜索!因此,在上面的例子中,你最好用一个子目录名称替换“/”,例如“/home”取决于你实际上想要搜索的地方。