Forum Moderators: bakedjake

Message Too Old, No Replies

Grep all subdirectories

Is there a command?

         

Clark

11:40 am on Nov 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I find myself using this command all the time:

grep "mysearchkeyword" *

and then to go down the tree I'll add:
grep "mysearchkeyword" */*
grep "mysearchkeyword" */*/*
grep "mysearchkeyword" */*/*/*
grep "mysearchkeyword" */*/*/*/*
grep "mysearchkeyword" */*/*/*/*/*
grep "mysearchkeyword" */*/*/*/*/*/*

until it says:
grep: */*/*/*/*/*: No such file or directory

I was wondering if there's a flag to make it grep through all subdirectories?

py9jmas

1:24 pm on Nov 13, 2005 (gmt 0)

10+ Year Member



It depends on which grep your Unix has. I think GNU grep has
rgrep
and
grep -r
for recursive grepping.

BSD grep supports -R, -r or -recursive for recursive grepping.

I'm not sure if recursive grep has filtered down to the likes of Solaris yet.

Clark

5:48 pm on Nov 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



grep -r worked. Thanks.

DamonHD

9:59 pm on Nov 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Clark,

My usual tool for this sort of thing is a combination of find and (e)grep, eg:

find . -type f -exec egrep pattern {} \;

And you can use some nice filtering in the find command.

Another variant, eg if you just want a list of names containing the pattern, is:

egrep -l `find . -type f -print`

Note the use of backquote rather than normal ' quotes.

Rgds

Damon