我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
当前回答
试试这个命令,它会给你包含你输入的模式的文件。
sudo grep -inr "your-pattern" /
在这里:i - 忽略案例区别,以便只有在案例中区别的字符相匹配。
n - 确保实际行内容的第一个字符在一张标签站上,以便标签的调整看起来正常。
r - 阅读每个目录下的所有文件,重复,仅按照符号链接,如果它们在命令线上。
其他回答
如果您严格想要使用查找,则使用查找 + 查找:
找到 /path/to/somewhere/ -type f -exec grep -nw 'textPattern' {} \;
步骤:
使用查找搜索文件,执行捕获所有。
这给了你找到文件的权力。
使用 -name 模式,如果您只想捕捉某些文件:
找到 /path/to/somewhere/ -type f -name \*.cpp -exec grep -nw 'textPattern' {} \;
您可以使用不同的搜索选项来改善您的文件搜索。
有一个名为The Silversearcher的新工具
sudo apt install silversearcher-ag
它与 Git 和其他 VCS 密切合作,所以您不会在.git 或其他目录中获得任何东西。
你可以简单地使用
ag "Search query"
它会为你完成任务!
要搜索字符串和输出,就是与搜索字符串相同的字符串:
for i in $(find /path/of/target/directory -type f); do grep -i "the string to look for" "$i"; done
吉:
for i in $(find /usr/share/applications -type f); \
do grep -i "web browser" "$i"; done
要显示包含搜索字符串的文件名:
for i in $(find /path/of/target/directory -type f); do if grep -i "the string to look for" "$i" > /dev/null; then echo "$i"; fi; done;
吉:
for i in $(find /usr/share/applications -type f); \
do if grep -i "web browser" "$i" > /dev/null; then echo "$i"; \
fi; done;
A Simple find can work handy. alias it in your ~/.bashrc 文件:
alias ffind find / -type f | xargs grep
开始一个新的终端和问题:
ffind 'text-to-find-here'
使用:
grep -Erni + "text you wanna search"
命令将重复搜索当前目录的所有文件和目录,并打印结果。
注意:如果您的格雷普输出没有颜色,您可以通过在您的壳源文件中使用格雷普=‘格雷普=颜色=总是’的标志来更改它。