Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
08-07-2012, 10:14 AM
|
#1
|
LQ Newbie
Registered: Jan 2012
Location: London, UK
Posts: 9
Rep:
|
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?
|
|
|
08-07-2012, 10:52 AM
|
#2
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,731
|
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)
|
|
|
08-07-2012, 10:54 AM
|
#3
|
Member
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 649
Rep:
|
*.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...
|
|
|
08-07-2012, 12:29 PM
|
#4
|
LQ Newbie
Registered: Jan 2012
Location: London, UK
Posts: 9
Original Poster
Rep:
|
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
*.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...
|
|
|
|
08-07-2012, 12:52 PM
|
#5
|
Member
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 649
Rep:
|
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 "{}".
|
|
|
08-07-2012, 12:55 PM
|
#6
|
LQ Newbie
Registered: Jan 2012
Location: London, UK
Posts: 9
Original Poster
Rep:
|
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
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 "{}".
|
|
|
|
08-07-2012, 06:22 PM
|
#7
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,397
|
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" {} \;
|
|
|
08-08-2012, 04:20 AM
|
#8
|
LQ Newbie
Registered: Jan 2012
Location: London, UK
Posts: 9
Original Poster
Rep:
|
The only thing I can get to work on Cygwin at the moment is:
find . -name "*.log" | grep -nH -r "my pattern" *.*
|
|
|
08-08-2012, 04:27 AM
|
#9
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,731
|
but you not need find to run grep:
grep -nH -r "my pattern" *.*
|
|
|
08-08-2012, 04:36 AM
|
#10
|
LQ Newbie
Registered: Jan 2012
Location: London, UK
Posts: 9
Original Poster
Rep:
|
Quote:
Originally Posted by pan64
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
|
|
|
08-08-2012, 07:14 PM
|
#11
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,397
|
[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.
|
|
|
08-09-2012, 01:33 AM
|
#12
|
Member
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 649
Rep:
|
Quote:
Originally Posted by acomber
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" '{}' \;
Last edited by 414N; 08-12-2012 at 08:08 AM.
Reason: Add a space hint
|
|
|
All times are GMT -5. The time now is 07:35 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|