我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
当前回答
此外,请参见 The Platinium Searcher,这类似于 The Silver Searcher,并写在 Go。
例子:
pt -e 'text to search'
其他回答
要搜索字符串和输出,就是与搜索字符串相同的字符串:
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;
如果使用在您的例子中找到相似的,最好添加 -s ( - 没有消息) 捕捉,并在命令结束时 2>/dev/null 避免大量的许可拒绝的消息由捕捉发送,并找到:
find / -type f -exec grep -sH 'text-to-find-here' {} \; 2>/dev/null
更快、更容易的工具在同一用途中存在 - 请参见下面. 最好尝试它们,只要它们在您的平台上可用,当然:
更快、更容易的替代品
RipGrep - 最快的搜索工具周围:
rg 'text-to-find-here' / -l
ag 'text-to-find-here' / -l
ACK:
ack 'text-to-find-here' / -l
警告:除非你真的不能避免,不要从“/”(根目录)搜索,以避免漫长而不有效的搜索!因此,在上面的例子中,你最好用一个子目录名称替换“/”,例如“/home”取决于你实际上想要搜索的地方。
找到与 xargs 是优先的,当有许多潜在的比赛可以通过. 它运行比其他选项更慢,但它总是工作. 正如一些发现,xargs 不处理文件与嵌入空间默认。
这里是 @RobEarl 的答案,增强,以便处理文件与空间:
find / -type f | xargs -d '\n' grep 'text-to-find-here'
下面是 @venkat 的答案,同样增强:
find . -name "*.txt" | xargs -d '\n' grep -i "text_pattern"
这里是 @Gert van Biljon的答案,同样增强:
find . -type f -name "*.*" -print0 | xargs -d '\n' --null grep --with-filename --line-number --no-messages --color --ignore-case "searthtext"
以下是 @LetalProgrammer 的答案,同样增强:
alias ffind find / -type f | xargs -d '\n' grep
这里是 @Tayab Hussain的答案,同样增强:
find . | xargs -d '\n' grep 'word' -sl
避免混乱并安装 ack-grep. 它消除了许多许可和引用问题。
apt-get install ack-grep
然后转到您想要搜索的目录,并运行下面的命令。
cd /
ack-grep "find my keyword"
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