我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
当前回答
这个捕捉命令会给你一个准确的结果,当你正在寻找特定的文本在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"
其他回答
我的使用案例是找到我写的Python代码,我写的JSON线的某种方式,我知道JSONL将是函数名称的一部分,到_JSON将出现在身体中,但没有更多的东西。
尽管有 50 个答案,但在同一文件中找到多个字符串(无论是在同一行中还是不在同一行中)并没有得到答案。
格雷普中的 -q 是安静的. 没有什么是打印,只有返回值是设置的. 因此,在结尾的 - 打印. 每个 -exec 只运行,如果前一个成功。
find . -type f -name "*.py" \
-exec grep -q -e 'to_json' {} \; \
-exec grep -q -e 'def\s.*jsonl' {} \; \
-print
找到与 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 -Erni + "text you wanna search"
命令将重复搜索当前目录的所有文件和目录,并打印结果。
注意:如果您的格雷普输出没有颜色,您可以通过在您的壳源文件中使用格雷普=‘格雷普=颜色=总是’的标志来更改它。
希望这就是帮助......
要在输出中提供更多信息,例如,在文件中获取字符号,文本可以如下:
find . -type f -name "*.*" -print0 | xargs --null grep --with-filename --line-number --no-messages --color --ignore-case "searthtext"
如果您有一个想法,文件类型是什么,您可以通过指定文件类型扩展来缩小您的搜索,在此情况下,.pas 或.dfm 文件:
find . -type f \( -name "*.pas" -o -name "*.dfm" \) -print0 | xargs --null grep --with-filename --line-number --no-messages --color --ignore-case "searchtext"
以下是选项的简短解释:
在搜索中,从当前目录中指定 - 名称“**” : 所有文件( - 名称“*.pas” -o - 名称“*.dfm” ) : 只有 *.pas 或 *.dfm 文件, 或 与 -o -type f 指定, 您正在寻找文件 -print0 和 -null 是关键的, 将文件名从搜索中转移到嵌入在 xargs 的文件名, 允许通过文件名。
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