我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
当前回答
找到与 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
其他回答
如果您严格想要使用查找,则使用查找 + 查找:
找到 /path/to/somewhere/ -type f -exec grep -nw 'textPattern' {} \;
步骤:
使用查找搜索文件,执行捕获所有。
这给了你找到文件的权力。
使用 -name 模式,如果您只想捕捉某些文件:
找到 /path/to/somewhere/ -type f -name \*.cpp -exec grep -nw 'textPattern' {} \;
您可以使用不同的搜索选项来改善您的文件搜索。
grep "text-to-find-here" file_name
或
grep "text-to-find-here" directory_path/*
如果你想搜索当前的目录:
grep "text-to-find-here" *
我很感动如何简单的吸引力使它与“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
它只是完美工作......
您也可以使用 awk:
awk '/^(pattern)/{print}' /path/to/find/*
模式是您想要在文件中匹配的行。
我的使用案例是找到我写的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