我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
当前回答
如果您在 Git 存储库中,您可以使用:
git grep something
其他回答
如果您严格想要使用查找,则使用查找 + 查找:
找到 /path/to/somewhere/ -type f -exec grep -nw 'textPattern' {} \;
步骤:
使用查找搜索文件,执行捕获所有。
这给了你找到文件的权力。
使用 -name 模式,如果您只想捕捉某些文件:
找到 /path/to/somewhere/ -type f -name \*.cpp -exec grep -nw 'textPattern' {} \;
您可以使用不同的搜索选项来改善您的文件搜索。
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
使用:
grep -Erni + "text you wanna search"
命令将重复搜索当前目录的所有文件和目录,并打印结果。
注意:如果您的格雷普输出没有颜色,您可以通过在您的壳源文件中使用格雷普=‘格雷普=颜色=总是’的标志来更改它。
如果您在 Git 存储库中,您可以使用:
git grep something
试试:
find . -name "*.txt" | xargs grep -i "text_pattern"