簡體   English   中英

使用grep搜索文件中的字符串,包括子文件夾

[英]Use grep to search for a string in files, include subfolders

我必須使用grep命令在文件中搜索特定文本並為此即時消息,但它僅在當前文件夾中搜索。我想要的是使用單個grep命令我可以在當前文件夾以及所有文件夾中搜索特定內容它的子文件夾。我該怎么辦???

man grep

   -R, -r, --recursive
          Read all  files  under  each  directory,  recursively;  this  is
          equivalent to the -d recurse option.

POSIX grep不支持遞歸搜索-grep的GNU版本支持。

find . -type f -exec grep 'pattern' {} \;

可以在任何POSIX兼容的UNIX上運行。

甚至更常見的是將find與xargs一起使用

 find <dir> -type f -name <shellglob> -print0 | xargs grep -0 

其中-print0-0,分別使用null字符分隔條目,以避免出現空格字符的文件名問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM