LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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

Reply
 
Search this Thread
Old 08-07-2012, 11:14 AM   #1
acomber
LQ Newbie
 
Registered: Jan 2012
Location: London, UK
Posts: 9

Rep: Reputation: Disabled
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?
 
Old 08-07-2012, 11:52 AM   #2
pan64
Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 5,139

Rep: Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363
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)
 
Old 08-07-2012, 11:54 AM   #3
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 609

Rep: Reputation: 180Reputation: 180
*.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...
 
Old 08-07-2012, 01:29 PM   #4
acomber
LQ Newbie
 
Registered: Jan 2012
Location: London, UK
Posts: 9

Original Poster
Rep: Reputation: Disabled
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 View Post
*.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...
 
Old 08-07-2012, 01:52 PM   #5
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 609

Rep: Reputation: 180Reputation: 180
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 "{}".
 
Old 08-07-2012, 01:55 PM   #6
acomber
LQ Newbie
 
Registered: Jan 2012
Location: London, UK
Posts: 9

Original Poster
Rep: Reputation: Disabled
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 View Post
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 "{}".
 
Old 08-07-2012, 07:22 PM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
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" {} \;
 
Old 08-08-2012, 05:20 AM   #8
acomber
LQ Newbie
 
Registered: Jan 2012
Location: London, UK
Posts: 9

Original Poster
Rep: Reputation: Disabled
The only thing I can get to work on Cygwin at the moment is:

find . -name "*.log" | grep -nH -r "my pattern" *.*
 
Old 08-08-2012, 05:27 AM   #9
pan64
Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 5,139

Rep: Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363
but you not need find to run grep:
grep -nH -r "my pattern" *.*
 
Old 08-08-2012, 05:36 AM   #10
acomber
LQ Newbie
 
Registered: Jan 2012
Location: London, UK
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
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
 
Old 08-08-2012, 08:14 PM   #11
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
[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.
 
Old 08-09-2012, 02:33 AM   #12
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 609

Rep: Reputation: 180Reputation: 180
Quote:
Originally Posted by acomber View Post
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 09:08 AM. Reason: Add a space hint
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Grep Recursive search directory depth option? timdvtemp Linux - Software 1 02-28-2011 04:49 PM
recursive grep speed allasso Linux - General 10 06-09-2010 08:22 PM
Sorting recursive 'ls' and 'grep' SirTristan Linux - Newbie 5 03-13-2008 03:39 PM
recursive grep xpucto Solaris / OpenSolaris 2 05-29-2007 10:57 AM
Recursive grep jimieee Linux - General 5 10-06-2003 11:13 AM


All times are GMT -5. The time now is 12:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration