grep, maybe

Ben Scott dragonhawk at gmail.com
Fri Oct 30 08:42:01 EDT 2009


On Fri, Oct 30, 2009 at 8:15 AM, Ken D'Ambrosio <ken at jots.org> wrote:
> ... might even be able to make Ben happy ...

  Oh, a challenge, eh?  ;-)

> cd /path/to/toplevel/dir
> find -type d | while read i

  Could be just:

	find /path/to/toplevel/dir -type d | while read i

>  grep moe "$i/*" > /dev/null && echo "$i"

  Definitely should be at least:

	grep moe "$i"/* > /dev/null && echo "$i"

  (Putting the star inside the quotes prevents the shell from globing
it, thus leading to grep trying to open a file named "*" in the
directory.)

  Could be:

	grep -q moe "$i"/* && echo "$i"

  (The -q switch to grep causes it to just exit with true on the first
match, making things faster if the match is earlier in the file. Also
avoids the need to throw away the output.)

> P.S.  It deals gracefully with spaces (I checked -- hadn't been sure about
> how the second "read" would work).

  Yah, "read" reads lines.  Lines are split into tokens on spaces and
put into parameters you give, but the last parameter gets the rest of
the line.  So with one parameter, you get the whole line.

> P.P.S.  It does appear that find is terminated for the second loop.  (Or
> so said the "ps" I threw into it to check.)

  Cool.  Thanks for checking.  And for letting us know.  :)

> P.^3S.  For millions of hits, your memory might start to bog.  In which
> case, if you expect that, it's definitely time to go with the temporary
> file thing.

  Or increase your swap space.  ;-)

-- Ben



More information about the gnhlug-discuss mailing list