Dependent Origination

find as grep

Posted on: October 24, 2011

my grep often ends in infinite loop and i am not exactly sure why — it must have something to do with our directory structure but after i gave it a couple seconds thought, i cannot think of any symbolic links that can cause such loops — anyway, today i really got bored with the infinite loop so i googled how to fix it — maybe with a flag of grep? turned out we can use find to print out the file names that a certain pattern appears and it runs so much faster than grep.

here is the command:

find . \( -name *.cc -o -name *.h \) -exec grep -l “search pattern” ‘{}’ \;

{} is the parameter that will translate into the current file name being considered, ” is used to prevent the shell replacing it with something else.

; is terminating the command that is passing into exec param and \ is used to escape it — can use single quote for the same purpose.

remember to have a space after \( and before \) otherwise find won’t recognize it as an Or expression — tried couple times to get it right.

-l to grep means only print the file names — so grep will stop after the first match.

Advertisements

1 Response to "find as grep"

when you copy from this post, remember to replace the single quote or double quote if they don’t look right to you — otherwise you could be getting lots of complains from grep saying the file is not found

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

October 2011
M T W T F S S
« Sep   Nov »
 12
3456789
10111213141516
17181920212223
24252627282930
31  

Twitter

Flickr Photos

%d bloggers like this: