UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » General
  • » Unix shell question: how to keep find out of certain folders?

#1 2008-02-07 11:54 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: Unix shell question: how to keep find out of certain folders?

Hi all,

After my co-worker decided he'd re-arrange our source base into a nice, logical, 8-level deep hierarchy of directories, my old trick to bring up all files containing the keyword FooBar:

vi `grep -l ../*.{cpp,h} FooBar`

Didn't work anymore.  No problem, I thought... I'll just write a little script that does the same thing, except for the whole subtree under the current folder.  So here's what I came up with:

[email protected]:~/bin> cat ~/bin/fvi
vi $(find . \( -name "*.cpp" -o -name "*.h" \) -exec grep -l "[email protected]" \{\} \;)

Which is great, now I can just enter "fvi FooBar" from the project's top level directory and up pops vi to edit all the files containing FooBar.  Handy for when I want to do a global change.

The only thing is, it's a bit slow.  In particular, our source tree contains several folders named '3rdparty', each of which contains a large amount of 3rd-party code.  I'd like to speed up my 'fvi' searches by telling 'find' to exclude any folder named '3rdparty' from its hierarchical search, but I can't figure out how to do that.  Does anyone know how?

Thanks,
Jeremy

Offline

#2 2008-02-08 12:50 AM

biologz
Administrator
From: Puking on the pavement
Registered: 2005-11-02
Posts: 396

Re: Unix shell question: how to keep find out of certain folders?


gethostbyintuition() is still a dream of mine

                                                 -- quoted from bash

Offline

#3 2008-02-08 01:02 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Unix shell question: how to keep find out of certain folders?

Offline

#4 2008-02-08 03:51 AM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: Unix shell question: how to keep find out of certain folders?

Aha, that put me on the right track... I think I've figured it out, and the magic incantation is:

[email protected]:~/bin> cat fvi
vi $(find . \( -name 3rdparty -prune -o -name "*.cpp" -o -name "*.h" \) -exec grep -l "[email protected]" \{\} \;)

Thanks guys :^)

Offline

#5 2008-02-08 02:36 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: Unix shell question: how to keep find out of certain folders?

Also, instead of doing the "-exec grep", it might speed things up a bit to instead just
do "-print | xargs grep"...  That'll only spawn a single grep (passed every matching
file at once) instead of a separate one for every file that matches your preconditions...
(Or, if you have potentially wacky filenames, you might want to use "-print0" and
"xargs -0"...)

Offline

#6 2008-02-08 07:10 PM

biologz
Administrator
From: Puking on the pavement
Registered: 2005-11-02
Posts: 396

Re: Unix shell question: how to keep find out of certain folders?

Okayyy...I think i was just OFF SUBJECT...I feel a bit humiliated right now. Glad you found the solution :-)


gethostbyintuition() is still a dream of mine

                                                 -- quoted from bash

Offline

#7 2008-08-18 02:45 AM

hkalilinks
Guest

Re: Unix shell question: how to keep find out of certain folders?

I finally found something to the question,thanks

  • Index
  • » General
  • » Unix shell question: how to keep find out of certain folders?

Board footer

Powered by FluxBB