search recursively in a particular filename
Hello,
Situation : I want to search for the word, 'confedit' in all makefiles in my system using grep.
I have tried :
I know of grep -r
I tried doing grep -r 'confedit' ./*Makefile 2>/dev/null
but it doesn't return anything.
The makefiles could be named as Makefile or makefile.
So please give me a file pattern which checks both.
Please advise,
Thanks.
Re: search recursively in a particular filename
Hi IAMTubby.
As far as I know, if you omit the filename, it would search recursively. Unfortunately, it would look for all files.
This may work:
Code:
find -type f -iname "makefile" -exec grep -H "confedit" '{}' \;
where:
- -type will filter only files
- -iname will match the filename "makefile" (case insensitive).
- -exec will execute a grep for each file found,
- -H in grep will print the filename with the match line.
Let us know how it goes.
Regards.
Re: search recursively in a particular filename
^ This won't output filenames where matching lines are found. Need to add -print.
Re: search recursively in a particular filename
Quote:
Originally Posted by
prodigy_
^ This won't output filenames where matching lines are found. Need to add -print.
Good catch! :)
The grep's option -H would do that even better (filename in same line as matched line).
Regards.
Re: search recursively in a particular filename
Quote:
Originally Posted by
papibe
find -type f -iname "makefile" -exec grep -H "confedit" '{}' \;
Thanks a lot papibe, it worked :)
I have 2 questions though:
1. I don't have to add the -print option,correct ? (I think it worked for me by adding -H alone, but since you said "good catch", I'm a bit confused)
2. Can you tell me what '{}' \; means towards the end of the command ?
Thanks.
Re: search recursively in a particular filename
Quote:
Originally Posted by
IAMTubby
1. I don't have to add the -print option,correct ?
You don't have to.
Quote:
Originally Posted by
IAMTubby
2. Can you tell me what '{}' \; means towards the end of the command ?
In the context of the -exec option the string '{}' represents the file that has been match. '\;' is the syntax to mark the end of the exec command.
Does that help?
Regards.
Re: search recursively in a particular filename
Quote:
Originally Posted by
papibe
In the context of the -exec option the string '{}' represents the file that has been match. '\;' is the syntax to mark the end of the exec command.
Thanks a lot papibe, it's clear now. :)
Re: search recursively in a particular filename
Also:
Code:
shopt -s globstar
grep -l "confedit" **/makefile
(with the "globstar" option "**" expands recursively to all the directories in the tree).
Re: search recursively in a particular filename
Quote:
Originally Posted by
papibe
find -type f -iname "makefile" -exec grep -H "confedit" '{}' \;
papibe, I'm sorry to reopen this thread again.
Ever since, you told me of the above command, I have been using it extensively with great success.
But I would like to know how you can give a pattern to search within the makefile.
To find all makefiles containing a reference to anything ending with .lo, I tried doing find -type f -iname "makefile" -exec grep -H "*.lo" '{}' \; but couldn't get the desired result.
Surprisingly, the pattern works when I'm only using find, as in, to find all files ending with .lo, this works
find ./ -name '*.lo' 2>/dev/null works and lists everything ending with .lo
Re: search recursively in a particular filename
Replace the '*' by backslash
Code:
find -type f -iname "makefile" -exec grep -H "\.lo" '{}' \;
"*.lo" looks for the string "*.lo", not for any string that ends with ".lo".