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.

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

  • is looking at other people's intentions, not their capabilities, and feeling much happier every day :) 11 months ago
  • is planning on how to spend the next two weeks until the new year, at home. 11 months ago
  • is going to have human company for Thanksgiving; a rare event for the past like twenty years. 1 year ago
  • living by myself again; a strange feeling in a bustling city with thirteen million people 1 year ago
  • just came back home after sixteen days on the road; when i set out, the plan was a mere three-day trip 1 year ago

Flickr Photos

IMG_3150

IMG_3175

IMG_3169

IMG_3139

IMG_3120

IMG_3111

IMG_3086

IMG_3079

IMG_3054

IMG_3048

More Photos
%d bloggers like this: