Search code examples
bashgrepfind

Linux find and grep command together


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

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... 

do you have any ideas?


Solution

  • 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.