如何使用“grep”命令查找包含子目录的文本

我想找到包含特定文本字符串的所有文件。 grep命令有效,但我不知道如何将它用于每个目录(我只能对我当前的目录使用它)。 我试过读man grep ,但没有得到任何帮助。

最好使用

 grep -rl "string" /path 

哪里

  • -r (或--recursive )选项用于遍历/path所有子目录,而
  • -l (或--files-with-matches )选项用于仅打印匹配文件的文件名,而不是匹配的行(这也可以提高速度,因为grep在第一次匹配时停止读取文件与此选项匹配) 。

如果你正在寻找文件中匹配的行,我最喜欢的命令是:

 grep -Hrn 'search term' path/to/files 
  • -H导致打印文件名(在搜索多个文件时隐含)
  • -r进行递归搜索
  • -n导致打印行号

path/to/files可以. 在当前目录中搜索

我觉得非常有用的其他选项:

  • -I忽略二进制文件(补充: -a将所有文件视为文本)
  • -Fsearch term视为文字,而不是正则表达式
  • -i做一个不区分大小写的搜索
  • --color=always强制颜色,即使管道less 。 要less支持颜色,需要使用-r选项:

     grep -Hrn search . | less -r 
  • --exclude-dir=dir对于排除.svn.git--exclude-dir=dir很有用。

示例输出

我相信你可以使用这样的东西:

 find /path -type f -exec grep -l "string" {} \; 

评论解释

find是一个命令,可以让您在给定路径的子目录中查找文件和其他对象,如目录和链接。 如果未指定文件名应满足的掩码,则枚举所有目录对象。

  • -type f指定它应该只处理文件,而不是目录等。
  • -exec grep指定对于每个找到的文件,它应该运行grep命令,将其文件名作为参数传递给它,方法是将{}替换为文件名

我的默认命令是

 grep -Rin string * 

我使用国会大厦’R’,因为ls用它来递归。 由于grep接受这两者,没有理由不使用它。

编辑:每个HVNSweeting,显然-R将遵循符号链接,其中-r不会。

如果你愿意尝试一些新的东西,那就试试吧。 递归搜索当前目录以查找string的命令是:

 ack string 

安装非常简单:

 curl http://betterthangrep.com/ack-standalone > ~/bin/ack && chmod 0755 !#:3 

(如果你已经有了目录~/bin ,它最好在你的PATH 。)

命令rgrep专门用于满足这种需求

如果没有,你可以这样做

 mkdir -p ~/bin cd ~/bin wget http://sdjf.esmartdesign.com/files/rgrep chmod +x rgrep 

您可以直接设置为默认的grep选项,如上所述。

我个人使用

 [[ ${#args} -lt 5 && "${args//[[:space:]]/}" == "-i" ]] && args="-Hin" args="${args:--Hns} --color=auto" 

相关主题: 如何始终使用rgrep颜色

更新2:

使用findgrep这一行命令修复了这个问题:

 $ find path_to_search_in -type f -exec grep -in searchString {} 2> /dev/null + 

--color=用于彩色输出:

 $ find path_to_search_in -type f \ -exec grep --color=always -in searchString {} 2>/dev/null + 

例:

 $ find /tmp/test/ -type f -exec grep --color=auto -in "Search string" {} 2>/dev/null + 

以下快照中运行的示例: snap1


更新1:

您可以尝试以下代码; 作为.bashrc.bash_aliases或脚本中的函数:

 wherein () { for i in $(find "$1" -type f 2> /dev/null); do if grep --color=auto -i "$2" "$i" 2> /dev/null; then echo -e "\033[0;32mFound in: $i \033[0m\n"; fi; done } 

用法: wherein /path/to/search/in/ searchkeyword

例:

 $ wherein ~/Documents/ "hello world" 

(注意:正如@enzotib在下面的评论中所建议的那样,这不适用于文件/目录,包括名称中的空格。)


原帖

要搜索字符串并使用搜索字符串输出该行:

 $ for i in $(find /path/of/target/directory -type f); do \ grep -i "the string to look for" "$i"; done 

例如:

 $ for i in $(find /usr/share/applications -type f); \ do grep -i "web browser" "$i"; done 

要显示包含搜索字符串的文件名:

 $ for i in $(find /path/of/target/directory -type f); do \ if grep -i "the string to look for" "$i" > /dev/null; then echo "$i"; fi; done; 

例如:

 $ for i in $(find /usr/share/applications -type f); \ do if grep -i "web browser" "$i" > /dev/null; then echo "$i"; \ fi; done; 

我使用xargs这是一个非常被低估的命令

 find ./ -type f -print0 | xargs -0 grep 'string_you_are_looking_for' 

find ./为您提供当前文件夹中所有文件的递归列表,然后将其传递给xargs,该xargs对每个文件执行grep命令

我知道这里有很多答案,但如果你想在搜索文件时添加其他限制,这里有一个替代方案:

 find . -type f -exec grep --quiet string_to_look_for {} ';' -print 

这是有效的,因为如果找到结果, grep将返回0,否则返回1。 例如,您可以找到1 MB大包含以下内容的文件:

 find . -type f -exec grep --quiet string_to_look_for {} ';' -size 1M -print 

对于多个要求,您可能希望使用GNU grep中存在的优化程序标志-O

用C,CPP代码搜索的脚本(find-in-code):

 #!/bin/sh find . \( -iname "*.c" -o -iname "*.cpp" -o -iname "*.h" \) -type f -print0 | xargs -0 grep --color -n "$1" 

使用:

 find-in-code "search string" 

grep ( GNU或BSD )

您可以使用grep工具以-r参数递归搜索当前文件夹,如:

 grep -r "pattern" . 

注意: -r – 递归搜索子目录。

要在特定文件中搜索,可以使用通配语法,例如:

 grep "class foo" **/*.c 

注意:通过使用globbing选项 ( ** ),它以递归方式扫描具有特定扩展名或模式的所有文件。 要启用此语法,请运行: shopt -s globstar 您也可以对所有文件使用**/*.* (隐藏和不包含隐藏)或任何其他模式。

如果您的错误是您的参数太长,请考虑缩小搜索范围,或使用find语法,例如:

 find . -name "*.php" -execdir grep -nH --color=auto foo {} ';' 

或者使用ripgrep

ripgrep

如果你正在处理更大的项目或大文件,你应该使用ripgrep ,比如:

 rg "pattern" . 

在GitHub项目页面上查看文档,安装步骤或源代码。

它比任何其他工具(如GNU / BSD grepucgagsiftackpt或类似工具)快得多,因为它建立在Rust的正则表达式引擎之上,该引擎使用有限自动机,SIMD和积极的文字优化来快速搜索。

它支持忽略.gitignore文件中指定的模式,因此单个文件路径可以同时与多个glob模式匹配。


您可以使用常用参数,例如:

  • -i – 不敏感的搜索。
  • -I – 忽略二进制文件。
  • -w – 搜索整个单词(与部分单词匹配相反)。
  • -n – 显示您的匹配项。
  • -C / --context (例如--context ) – 增加上下文,因此您可以看到周围的代码。
  • --color=auto – 标记匹配的文本。
  • -H – 显示找到文本的文件名。
  • -c – 显示匹配行的数量。 可与-H组合使用。