我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
当前回答
如果您严格想要使用查找,则使用查找 + 查找:
找到 /path/to/somewhere/ -type f -exec grep -nw 'textPattern' {} \;
步骤:
使用查找搜索文件,执行捕获所有。
这给了你找到文件的权力。
使用 -name 模式,如果您只想捕捉某些文件:
找到 /path/to/somewhere/ -type f -name \*.cpp -exec grep -nw 'textPattern' {} \;
您可以使用不同的搜索选项来改善您的文件搜索。
其他回答
重复和案例不敏感的捕捉与线号:
grep -inr "Text" folder/to/be/searched/
grep "text-to-find-here" file_name
或
grep "text-to-find-here" directory_path/*
如果你想搜索当前的目录:
grep "text-to-find-here" *
试试这个命令,它会给你包含你输入的模式的文件。
sudo grep -inr "your-pattern" /
在这里:i - 忽略案例区别,以便只有在案例中区别的字符相匹配。
n - 确保实际行内容的第一个字符在一张标签站上,以便标签的调整看起来正常。
r - 阅读每个目录下的所有文件,重复,仅按照符号链接,如果它们在命令线上。
这个捕捉命令会给你一个准确的结果,当你正在寻找特定的文本在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"
安德鲁·加兰特的博客声称:“Ripgrep 比 {grep, ag, git grep, ucg, pt, sift} 更快”(一些人分享的声明,这就是为什么一个功能比较是有用的)。 特别有兴趣的是他关于 regex 实施和漏洞的部分. 下一个命令搜索所有文件,包括隐藏和可执行: $ rg -uuu foobar 银搜索器(ag)声称它是 5-10 倍的速度。
虽然可以扫描外部程序,执行搜索,阻止其输出并处理它,呼叫API是电源和性能的途径。