LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 12-07-2007, 03:27 PM   #1
new_2_unix
LQ Newbie
 
Registered: Oct 2007
Posts: 26

Rep: Reputation: 15
question about grep


hi,

i have a simple question. i want to grep for a string recursively in all the files in a directory structure. i can achieve this objective almost completely with:

Code:
find . -exec grep "Search String" {} \; > myreport
This works just fine, the only problem is that it doesn't tell the names of the files in which the "Search String" was found.

Using the -print before the -exec results in a whole list of file names which do not contain the "Search String".

any guidance on how to do this will be very helpful. thanks!

Last edited by new_2_unix; 12-07-2007 at 03:32 PM.
 
Old 12-07-2007, 03:43 PM   #2
rtspitz
Member
 
Registered: Jan 2005
Location: germany
Distribution: suse, opensuse, debian, others for testing
Posts: 307

Rep: Reputation: 33
grep -R "string" * >out.txt

find . -exec grep "string" {} \; -exec echo {} \; >out.txt
 
Old 12-07-2007, 03:55 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by new_2_unix View Post
hi,

i have a simple question. i want to grep for a string recursively in all the files in a directory structure. i can achieve this objective almost completely with:

Code:
find . -exec grep "Search String" {} \; > myreport
This works just fine, the only problem is that it doesn't tell the names of the files in which the "Search String" was found.

Using the -print before the -exec results in a whole list of file names which do not contain the "Search String".

any guidance on how to do this will be very helpful. thanks!
Code:
find . -exec grep -H "Search String" {} \; > myreport


Cheers,
Tink
 
Old 12-07-2007, 04:15 PM   #4
rsashok
Member
 
Registered: Nov 2006
Location: USA, CA
Distribution: RedHat, Debian
Posts: 202

Rep: Reputation: 31
Another way:

Code:
find . -type f -name * | xargs grep "string"
 
Old 12-07-2007, 04:22 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by rsashok View Post
Another way:

Code:
find . -type f -name * | xargs grep "string"
Almost....
Code:
find . -type f -name \* | xargs grep "string"


Cheers,
Tink
 
Old 12-07-2007, 04:45 PM   #6
new_2_unix
LQ Newbie
 
Registered: Oct 2007
Posts: 26

Original Poster
Rep: Reputation: 15
thanks everyone!
that was very helpful.
 
  


Reply



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
maybe a grep question: me210 Linux - Software 3 01-19-2007 12:13 PM
grep question djgerbavore Linux - Newbie 3 11-04-2004 10:53 AM
Question about Grep irfanhab Linux - Newbie 6 08-23-2004 02:57 PM
grep question vasanthraghavan Programming 3 04-23-2004 12:32 AM
grep question keyops Programming 2 04-09-2004 09:17 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:53 PM.

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
Open Source Consulting | Domain Registration