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

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

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

当前回答

使用 grep -ilR:

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

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

其他回答

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

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;

这个捕捉命令会给你一个准确的结果,当你正在寻找特定的文本在Linux -

grep -inRsH “文本要被搜索” /path/to/dir(它可以是“。

i stands for ignore case distinctions R stands for recursive and it also includes symlinks. It is better to use 'R' instead of 'r' n stands for "it will print line number." s stands for "suppress error messages" H stands for "it will print the file name for each match"

我很感动如何简单的吸引力使它与“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

它只是完美工作......

grep -insr "pattern" *

i: 在 PATTERN 和输入文件中忽略案例区别. n: 在输入文件中预定每个输出线的 1 行号. s: 删除关于不存在或不可读的文件的错误消息. r: 阅读每个目录下的所有文件,重复。

找到与 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