[英]Unix command to search for text string in all files
How do I get unix to search inside ALL files for a the string "hello"? 我如何获得unix在所有文件中搜索字符串“ hello”?
I've tried the following but it doesn't work: 我尝试了以下方法,但是不起作用:
sudo grep "hello" | find / -name "*" 2>/dev/null
Does anyone have any other ideas? 还有其他想法吗?
Thank you. 谢谢。
Maybe this one? 也许这个?
sudo cd / && grep -rn "hello" *
EDIT: the 'n' option of course is not needed - it simply displays the line numbers and I find it nice. 编辑:当然不需要'n'选项-它只是显示行号,我觉得很好。 The 'r' option tells grep to perform recursive search within directories. 'r'选项告诉grep在目录内执行递归搜索。
Use 使用
grep -r 'pattern' /
If your grep
supports -r
, (GNU grep
does); 如果你grep
支持-r
,(GNU grep
一样); if not use 如果不使用
find / -type f -exec grep 'pattern' {} +
If your find
supports -exec
with +
, otherwise use 如果find
支持+
-exec
,否则使用
find / -type f -printf '%p\0' | xargs -0 grep 'pattern'
If your find
supports -printf
and your xargs
supports -0
, or 如果您的find
支持-printf
而xargs
支持-0
,或者
find / -type f -print0 | xargs -0 grep 'pattern'
If your find
supports only -print0
and your xargs
supports -0
. 如果您的find
仅支持-print0
而xargs
支持-0
。 In all other cases fall back on 在所有其他情况下,
find / -type f | xargs grep 'pattern'
This is maximally compatible, with the caveat that certain unusual file names will fail to be grepped and crafted file names could pose a security risk. 这是最大程度地兼容的,但需要注意的是,某些不寻常的文件名将无法被抓取,并且精心设计的文件名可能会带来安全风险。
Note that you will have to be root in order to be sure of searching all files and that the search will be case-sensitive unless you add -i
to grep
. 请注意,您必须是root用户才能确保搜索所有文件,并且除非将-i
添加到grep
否则搜索将区分大小写。
这个?
find . -exec grep -H "hello" {} \;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.