Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash find/grep列出找到的包含特定字符串的特定文件_Bash_Shell_Command Line_Grep - Fatal编程技术网

Bash find/grep列出找到的包含特定字符串的特定文件

Bash find/grep列出找到的包含特定字符串的特定文件,bash,shell,command-line,grep,Bash,Shell,Command Line,Grep,我有一个根目录,需要对其运行find和/或grep命令以返回包含特定字符串的文件列表 下面是一个文件和目录设置的示例。事实上,这个根目录包含很多子目录,每个子目录都有很多子目录和文件,但我希望这个示例能够理解我的观点 从根目录开始,我需要遍历每个子目录,特别是进入subdir/目录,并在file.html中查找字符串“example:”。如果找到结果,我希望它打印出file.html的完整路径,例如website_two/subdir/file.html 我认为将搜索限制在subdir/fil

我有一个根目录,需要对其运行find和/或grep命令以返回包含特定字符串的文件列表

下面是一个文件和目录设置的示例。事实上,这个根目录包含很多子目录,每个子目录都有很多子目录和文件,但我希望这个示例能够理解我的观点

从根目录开始,我需要遍历每个子目录,特别是进入subdir/目录,并在file.html中查找字符串“example:”。如果找到结果,我希望它打印出file.html的完整路径,例如website_two/subdir/file.html

我认为将搜索限制在subdir/file.html将大大提高此操作的速度

我对find和grep命令不是很熟悉,但是我尝试了下面的命令,但没有成功,但是我真的不知道如何解决它

find . -name "file.html" -exec grep -HI "example:" {} \;

编辑:我知道这可能被标记为重复,但我想我的问题更像是如何告诉命令只搜索特定路径中的特定文件,在所有根->级目录中循环。

find./-type f-iname file.html-exec grep-l“示例:{}\+

grep-Rl”示例:“./| grep-iE”file.htm(l)*$”
将实现此功能

引用GNU Grep 2.25手册页:


我知道这可能被标记为重复,但我想我的问题更像是如何告诉命令只搜索特定路径中的特定文件,在所有根->级目录中循环。可能是因为没有匹配(
-I
表示不区分大小写),否则替换final
\将启动更少的进程,它将被命令行中可以容纳的尽可能多的参数所取代。我编写了一个递归bash(也使用sh进行了测试)函数,该函数跟踪给定的目录树和给定字符串的给定名称的文件。看见您可以修改
grep
命令以满足您的需要。您的
find-exec grep
有什么问题?我真的不明白为什么它不起作用。这将读取目录树中的所有文件,即使我们只对那些具有已知名称的文件感兴趣。可能还有很多其他文件。
grep | grep
解决方案将只打印出与
file.htm(l)*$
模式匹配的文件。它比
find
要慢20%左右。使用
find
snippet编辑answed
  -R, --dereference-recursive
         Read all files under each directory, recursively.  Follow all symbolic links, unlike -r.

  -l, --files-with-matches
         Suppress normal output; instead print the name of each input file from which output would normally have
         been printed.  The scanning will stop on the first match.

 -i, --ignore-case
         Ignore case distinctions in both the PATTERN and the input files.

 -E, --extended-regexp
         Interpret PATTERN as an extended regular expression.