我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
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 - 新命令线搜索工具
其他回答
我很感动如何简单的吸引力使它与“rl”:
grep -rl 'pattern_to_find' /path/where/to/find
-r to recursively find a file / directory inside directories..
-l to list files matching the 'pattern'
使用“r”而不“l”以查看文件名跟随文本中的模式!
grep -r 'pattern_to_find' /path/where/to/find
它只是完美工作......
试试这:
find . | xargs grep 'word' -sl
要搜索字符串和输出,就是与搜索字符串相同的字符串:
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 -inr "Text" folder/to/be/searched/
您可以使用 ack. 它就像输入源代码. 您可以扫描您的整个文件系统。
只是做:
ack 'text-to-find-here'
在你的根目录中。
您也可以使用常规表达式,指定文件类型等。
更新
我刚刚发现The Silver Searcher,这类似于Ack,但比它快3至5倍,甚至忽略了来自.gitignore 文件的模式。