LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   question about grep (https://www.linuxquestions.org/questions/linux-newbie-8/question-about-grep-605254/)

new_2_unix 12-07-2007 03:27 PM

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!

rtspitz 12-07-2007 03:43 PM

grep -R "string" * >out.txt

find . -exec grep "string" {} \; -exec echo {} \; >out.txt

Tinkster 12-07-2007 03:55 PM

Quote:

Originally Posted by new_2_unix (Post 2983513)
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

rsashok 12-07-2007 04:15 PM

Another way:

Code:

find . -type f -name * | xargs grep "string"

Tinkster 12-07-2007 04:22 PM

Quote:

Originally Posted by rsashok (Post 2983547)
Another way:

Code:

find . -type f -name * | xargs grep "string"

Almost....
Code:

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


Cheers,
Tink

new_2_unix 12-07-2007 04:45 PM

thanks everyone!
that was very helpful.


All times are GMT -5. The time now is 05:52 AM.