1821051 Members
2754 Online
109631 Solutions
New Discussion юеВ

Re: recursive grep

 
SOLVED
Go to solution
Singaram
Advisor

recursive grep

Is there any way to grep for a word in the entire path rather than current directory

Singaram
8 REPLIES 8
Mark Greene_1
Honored Contributor

Re: recursive grep

try this:

for _FILE in `ls -R /dir/path`; do
grep $_STRING $_FILE >>$_GREP.OUT
done

where /dir/path is the starting directory you want to search in, _GREP.OUT is a file that you want the output to go to and _STRING is the value you are searching on.

mark
the future will be a lot like now, only later
Pete Randall
Outstanding Contributor
Solution

Re: recursive grep

Singaram,

I think what you need can be done with find:

"find /starting_point -exec grep "word" {} \;"
-or-
"find /starting-point |xargs grep "word"


Pete



Pete
Pete Randall
Outstanding Contributor

Re: recursive grep

Minor correction:

use grep -l rather than just grep.


Pete



Pete
Brian Bergstrand
Honored Contributor

Re: recursive grep

Download gnu grep. It supports recursive searches. Then you can do the following:

gnugrep -r "pattern"

And all files in all sub-dirs will be searched.

I wish HP would include this as part of the base OS.

HTH.
Brian Bergstrand
Honored Contributor

Re: recursive grep

Singaram
Advisor

Re: recursive grep

Thanks to all

I used find / 2>/dev/null | xargs grepl -l word 2>/dev/null

Is there any way to avoid 2>/dev/null two times (One for find and one for grep)

Singaram
john korterman
Honored Contributor

Re: recursive grep

Hi,
you can execute it as a group command, e.g.:
# (find / -type f | xargs grep -l word )2>/dev/null

the "type -f" is good to include as it makes find skip reading directory files.

regards,
John K.
it would be nice if you always got a second chance
Bruno Ganino
Honored Contributor

Re: recursive grep

Singaram,
find "path_dir" -exec grep "word_find" {} \;
Bruno

Torino (Turin) +2H