简体   繁体   English

Unix命令在所有文件中搜索文本字符串

[英]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支持-printfxargs支持-0 ,或者

find / -type f -print0 | xargs -0 grep 'pattern'

If your find supports only -print0 and your xargs supports -0 . 如果您的find仅支持-print0xargs支持-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.

相关问题 如何在unix(osx)中搜索/替换一堆文本文件 - How to search/replace a bunch of text files in unix (osx) 是否有热键可以选择我在unix命令中输入的所有文本? - Is there a hotkey to select all the text I've typed in a unix command? 如何使用命令行工具替换由Unix / MacOS上的多行正则表达式搜索模式选择的文件中的字符串 - How to replace a string in a file selected by a multi-line regex search pattern on Unix/MacOS with command line tools 命令使用Unix Terminal打开当前文件夹中具有指定扩展名的所有文件吗? - Command to open all files with a specified extension the current folder using Unix Terminal? 命令行打印文本文件以适合Unix / Mac OS上的一页 - Command line printing of text files to fit one page on Unix / Mac OS 搜索所有文件和所有文件 - Search all files and in all files Mac OSX Unix Bash脚本将文件夹中的所有文件捕获到一个文本文件中,最好使用文件顶部的标签 - Mac OSX Unix Bash Script to cat all files in a folder to one text file, preferrably with a label at the top of the file 在 Unix 中查找具有特定扩展名的所有文件? - Finding all files with certain extension in Unix? 用于Java程序的UNIX命令,从另一个目录输入文本 - UNIX Command for Java program, input text from another directory 如何递归删除 UNIX 目录中的所有隐藏文件? - How do you recursively delete all hidden files in a directory on UNIX?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM