我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
当前回答
Silver Searcher 是一个可怕的工具,但 ripgrep 可能甚至更好。
它在Linux、Mac和Windows上运行,并在几个月前在Hacker News上编写(它有一个链接到Andrew Gallant的博客,其中有一个GitHub链接):
Ripgrep - 新命令线搜索工具
其他回答
试试这:
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;
格雷普是你的好朋友来实现这一点。
grep -r <text_fo_find> <directory>
如果你不在乎要找到文本的案例,那么使用:
grep -ir <text_to_find> <directory>
你的命令是正确的. 你只需要添加 -l 来抓住:
find / -type f -exec grep -l 'text-to-find-here' {} \;
做下列事:
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 参数排除一个或多个目录。
这对我来说非常好,为了实现几乎与你的同样的目的。
更多选项,看看男人抓住。