Not grep!

While reviewing some shell scripts last week, I saw the infamous find | grep:

$ /usr/bin/find /foo -type f | egrep -v \*.inp

I am not real sure why more people don’t leverage the logic operations build into find:

$ /usr/bin/find /foo -type f -not -name \*.inp

This saves a fork() and exec(), and should be a bit faster. I am curious if folks use grep because it’s easier to read, or because they don’t know about the logic operations built into find. I shall need to investigate …

5 Comments

Andy Miller  on February 10th, 2007

You mean the logic operations built into find? :-) (Since the object is to bypass the fork to grep)

I think one of the reasons is that the find syntax can be somewhat arcane, and even the newest sysadmins know what grep does. By the the time you realize find can do so much more, find | grep is eternally committed to muscle memory.

More seasoned sysadmins might realize (after maintaining one too many ‘what the hell was he thinking when he wrote this?’, or perhaps even more commonly, ‘what the hell was I thinking when I wrote this?’ type scripts) that often clarity wins over efficiency, especially in non-time critical applications.

(So I guess I’m chiming on your poll for a data point of “both :-) )

-Andy

matty  on February 10th, 2007

Ooops — thanks for the correction Andy!

Thomas  on February 12th, 2007

I have just begin digging through the scripts that HP OVO uses for monitoring, this is how they try to determine if knfsd is installed or not: rpm -qa | grep knfsd | wc -l` -eq 1 && ${USE_KERNEL_NFSD} != “no”. There is plenty more terrible ‘newbe’ stuff in their scritps…

Ceri Davies  on April 13th, 2007

Perhaps it’s because the commands that you quoted do completely opposite things ;-)

matty  on April 14th, 2007

Hi Ceri,

Your right! I forgot to type in the egrep “-v” option. Oops #2!

- Ryan

Leave a Comment