我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
当前回答
试试这
find . -type f -name some_file_name.xml -exec grep -H PUT_YOUR_STRING_HERE {} \;
其他回答
可以使用,即使我们不寻找一个链条。
只是跑步,
grep -RIl "" .
将打印到所有文本文件的路径,即只有可打印的字符的文件。
这个捕捉命令会给你一个准确的结果,当你正在寻找特定的文本在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"
找到与 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
重复和案例不敏感的捕捉与线号:
grep -inr "Text" folder/to/be/searched/
如果您的捕捉不支持重复搜索,您可以将搜索与 xargs 相结合:
find / -type f | xargs grep 'text-to-find-here'
我觉得这比找到 -exec 的格式更容易记住。
这将产生相匹配线的文件名和内容,例如。
/home/rob/file:text-to-find-here
您可能想添加到捕捉的选项旗帜:
-i - 案例不敏感搜索 -l - 只输出相匹配的行输出(而不是相匹配的行输出)