我如何在其文件内容中找到包含一个特定的文本序列的所有文件?

下一个不起作用. 它似乎显示系统中的每个单一文件。

find / -type f -exec grep -H 'text-to-find-here' {} \;

当前回答

试试这:

find / -type f -name "*" -exec grep -il "String_to_search" {} \;

for i in /*;do grep -Ril "String_to_search" $i;done 2> /dev/null

其他回答

要搜索字符串和输出,就是与搜索字符串相同的字符串:

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;

如果您严格想要使用查找,则使用查找 + 查找:

找到 /path/to/somewhere/ -type f -exec grep -nw 'textPattern' {} \;

步骤:

使用查找搜索文件,执行捕获所有。

这给了你找到文件的权力。

使用 -name 模式,如果您只想捕捉某些文件:

找到 /path/to/somewhere/ -type f -name \*.cpp -exec grep -nw 'textPattern' {} \;

您可以使用不同的搜索选项来改善您的文件搜索。

做下列事:

grep -rnw '/path/to/somewhere/' -e 'pattern'

-r 或 -R 是重复的, -n 是字符号, -w 是相匹配整个单词的。 -l (较低的 L 案例) 可以添加到只提供相匹配文件的文件名。

与此同时, - 排除, - 包括, - 排除-dir 旗帜可以用于有效的搜索:

这只会通过具有.c 或.h 扩展的文件进行搜索: grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern" 这将排除与.o 扩展结束的所有文件进行搜索: grep --exclude=\*.o -rnw '/path/to/somewhere/' -e "pattern" 对于目录,可以使用 --exclude-dir 参数排除一个或多个目录。

这对我来说非常好,为了实现几乎与你的同样的目的。

更多选项,看看男人抓住。

您可以使用 ripgrep 将遵守默认项目的.gitignore 文件。

此分類上一篇

拒绝许可的错误:

rg -i rustacean 2> /dev/null

将默认错误(stderr)输出转向 /dev/null。

使用 grep -ilR:

grep -Ril "text-to-find-here" /

i 代表忽略案例(可选在您的案例中)。R 代表重复。l 代表“显示文件名,而不是结果本身”。 / 代表在您的机器的根源开始。