bash Linux find 和 grep 命令一起使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21763904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:40:32  来源:igfitidea点击:

Linux find and grep command together

bashgrepfind

提问by user3307574

I am trying to find a command or create a Linux script that can do this two comands and list the otuput

我试图找到一个命令或创建一个 Linux 脚本来执行这两个命令并列出 otuput

find . -name '*bills*' -print

this prints all the files

这将打印所有文件

./may/batch_bills_123.log
./april/batch_bills_456.log
..

from this result I want to do a grep for a word I do this manually right now

从这个结果我想为一个词做一个 grep 我现在手动做这个

grep 'put' ./may/batch_bill_123.log 

and get

并得到

sftp > put oldnet_1234.lst

I would hope to get the file name and its match.

我希望获得文件名及其匹配项。

./may/batch_bills_123.log   sftp > put oldnet_1234.lst
..
..
and so on... 

any ideas?

有任何想法吗?

回答by BMW

You are looking for -Hoption in gnu grep.

您正在-Hgnu grep中寻找选项。

find . -name '*bills*' -exec grep -H "put" {} \;

Here is the explanation

这是解释

    -H, --with-filename
      Print the filename for each match.

回答by Reinstate Monica Please

Now that the question is clearer, you can just do this in one grep

现在问题更清楚了,您可以在一个grep 中完成此操作

grep -R --include "*bills*" "put" .

With relevant flags

带有相关标志

   -R, -r, --recursive
          Read  all  files  under  each  directory,  recursively;  this is
          equivalent to the -d recurse option.
   --include=GLOB
          Search only files whose base name matches GLOB  (using  wildcard
          matching as described under --exclude).

回答by Dalin

Or maybe even easier

或者甚至更容易

grep -R put **/*bills*

The **glob syntax means "any depth of directories". It will work in Zsh, and I think recent versions of Bash too.

**水珠语法手段“目录中的任何深度”。它可以在 Zsh 中工作,我认为 Bash 的最新版本也是如此。