Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux find and grep command together

Tags:

grep

find

bash

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?

like image 474
user3307574 Avatar asked Feb 13 '14 19:02

user3307574


People also ask

How do you grep recursively?

Recursive Search To recursively search for a pattern, invoke grep with the -r option (or --recursive ). When this option is used grep will search through all files in the specified directory, skipping the symlinks that are encountered recursively.

Can I grep for two things?

Grep is a powerful utility available by default on UNIX-based systems. The name stands for Global Regular Expression Print. By using the grep command, you can customize how the tool searches for a pattern or multiple patterns in this case. You can grep multiple strings in different files and directories.


1 Answers

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. 
like image 180
BMW Avatar answered Oct 07 '22 12:10

BMW