LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   problem using recursive grep (-r option) (https://www.linuxquestions.org/questions/linux-newbie-8/problem-using-recursive-grep-r-option-4175420840/)

acomber 08-07-2012 10:14 AM

problem using recursive grep (-r option)
 
If I use the command:

grep -nH -r "my pattern" *.*

I get results back as expected

But if the file pattern is like this:

grep -nH -r "my pattern" *.log

I get no results back (Even though I have a ton of files with this pattern with .log file extension).

Am I doing something wrong?

pan64 08-07-2012 10:52 AM

yes, *.log will be evaluated by the shell, not by grep. So if you have no *.log file in the current dir *.log will remain *.log and grep will try to search pattern in the file named *.log. You can check how the shell evaluates commands by: echo <command>, in your case echo grep -nH -r "pattern" *.log. The option -r has no effect if you gave an invalid file/dir argument (as *.log)

414N 08-07-2012 10:54 AM

*.log is expanded by the shell as a list of files ending with that suffix, so no directories will be recursed (unless they all have the .log suffix).
You could use another grep to filter files by extension:
Code:

grep -nH -r "my pattern" * | grep -e '^.*\.log:'
or resort to find:
Code:

find . -name "*.log" -exec grep -nH "my pattern" {} \;
Although it should work, I guess this is not the most efficient solution, though...

acomber 08-07-2012 12:29 PM

re: problem using recursive grep (-r option)
 
Hmmm I am beginning to prefer the find option now. But

find . -name "*.log" -exec grep -nH "my pattern" {} \;

or

find . -name "*.log" -exec grep -nH "my pattern" \{\} \;

doesn't find anything???


Quote:

Originally Posted by 414N (Post 4748199)
*.log is expanded by the shell as a list of files ending with that suffix, so no directories will be recursed (unless they all have the .log suffix).
You could use another grep to filter files by extension:
Code:

grep -nH -r "my pattern" * | grep -e '^.*\.log:'
or resort to find:
Code:

find . -name "*.log" -exec grep -nH "my pattern" {} \;
Although it should work, I guess this is not the most efficient solution, though...


414N 08-07-2012 12:52 PM

Well, I used "." as path in my example because on your grep examples you looked for files in the current directory.
Of course, if the directory to search is different than the present one, you should specify it instead of ".".
By the way, there's no need to escape the "{}".

acomber 08-07-2012 12:55 PM

Re: problem using recursive grep (-r option)
 
For some reason I get:
find "." -name "*.log" -exec grep -nH "message RequestAnswerCall" {} \;
find: missing argument to `-exec'

Any ideas why?

Quote:

Originally Posted by 414N (Post 4748281)
Well, I used "." as path in my example because on your grep examples you looked for files in the current directory.
Of course, if the directory to search is different than the present one, you should specify it instead of ".".
By the way, there's no need to escape the "{}".


chrism01 08-07-2012 06:22 PM

Must depend on your distro (or $PATH).
Works fine for me on RHEL5
Code:

find "." -name "*.log" -exec grep -nH "message RequestAnswerCall" {} \;

# Incidentally, no need to quote dir unless it has spaces
# and I prefer single quotes on filename pattern to prevent
# interpolation by shell
# it still doesn't throw an error
 find . -name '*.log' -exec grep -nH "message RequestAnswerCall" {} \;


acomber 08-08-2012 04:20 AM

The only thing I can get to work on Cygwin at the moment is:

find . -name "*.log" | grep -nH -r "my pattern" *.*

pan64 08-08-2012 04:27 AM

but you not need find to run grep:
grep -nH -r "my pattern" *.*

acomber 08-08-2012 04:36 AM

Quote:

Originally Posted by pan64 (Post 4748783)
but you not need find to run grep:
grep -nH -r "my pattern" *.*

Yes agreed that works. But read above and you will see why I posted :)

chrism01 08-08-2012 07:14 PM

[quote]
The only thing I can get to work on Cygwin
[quote]
This(!), you should have mentioned it in your first post.
Cygwin is a port of *nix tools to MS and may vary from eg native GNU/Linux versions.
Try the man pages if it comes with those.

414N 08-09-2012 01:33 AM

Quote:

Originally Posted by acomber (Post 4748283)
For some reason I get:
find "." -name "*.log" -exec grep -nH "message RequestAnswerCall" {} \;
find: missing argument to `-exec'

Any ideas why?

Try protecting the braces with single quotes and make sure there's a space between them and "\;":
Code:

find "." -name "*.log" -exec grep -nH "message RequestAnswerCall" '{}' \;


All times are GMT -5. The time now is 07:19 AM.