Linux find and grep command together

BashGrepFind

Bash Problem Overview


I am trying to find a command or create a Linux script that can do this two comands and list the 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 '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?

Bash Solutions


Solution 1 - Bash

You are looking for -H option in gnu grep.

find . -name '*bills*' -exec grep -H "put" {} \;
Here is the explanation
    -H, --with-filename
      Print the filename for each match.

Solution 2 - Bash

Now that the question is clearer, you can just do this in one [tag: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).

Solution 3 - Bash

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.

Solution 4 - Bash

grep -l "$SEARCH_TEXT" $(find "$SEARCH_DIR" -name "$TARGET_FILE_NAME")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3307574View Question on Stackoverflow
Solution 1 - BashBMWView Answer on Stackoverflow
Solution 2 - BashReinstate Monica PleaseView Answer on Stackoverflow
Solution 3 - BashDalinView Answer on Stackoverflow
Solution 4 - BashF. P. FreelyView Answer on Stackoverflow