dbaspot
Tags Register FAQ Calendar Search Today's Posts Mark Forums Read

How to improve 'find' by skipping .svn directories? - shell

This is a discussion on How to improve 'find' by skipping .svn directories? - shell ; Hi All, I am developing with SVN and have to find some text in all *.c/*.h/*.S files. find ! -name '.svn' -a -name '*.c' | xargs grep ... But the performance is not very good. And I try find ! ...


Home > Database Forum > Operating Systems > shell > How to improve 'find' by skipping .svn directories?

Reply

 

LinkBack Thread Tools Display Modes
  #1  
Old 11-14-2008, 06:50 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default How to improve 'find' by skipping .svn directories?

Hi All,

I am developing with SVN and have to find some text in all *.c/*.h/*.S
files.

find ! -name '.svn' -a -name '*.c' | xargs grep ...
But the performance is not very good.
And I try
find ! -name '.svn'
the speed is not lower than the case where there are no any .svn
directories under my working directory.
Is there any option for `find' to improve performance?

Best Regards,
PRC
Nov 14, 2008
Reply With Quote
  #2  
Old 11-14-2008, 07:12 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: How to improve 'find' by skipping .svn directories?

On 2008-11-14, PRC wrote:
> Hi All,
>
> I am developing with SVN and have to find some text in all *.c/*.h/*.S
> files.
>
> find ! -name '.svn' -a -name '*.c' | xargs grep ...
> But the performance is not very good.
> And I try
> find ! -name '.svn'
> the speed is not lower than the case where there are no any .svn
> directories under my working directory.
> Is there any option for `find' to improve performance?
>
> Best Regards,
> PRC
> Nov 14, 2008


I don't know the exact syntax but you have to tell find not to check
subdirectories of .svn
Your instruction isn't ignoring the .svn directories, it's just not
displaying files named '.svn'
Reply With Quote
  #3  
Old 11-14-2008, 08:35 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: How to improve 'find' by skipping .svn directories?

PRC wrote:
>
> I am developing with SVN and have to find some text in all *.c/*.h/*.S
> files.


I also do this regularly. I strongly recommend to use zsh as the
interactive shell (it has _so_ many useful interactive features).
For your task:
grep whatever **/*.{c,h,S}
(OK, this excludes even all ".*" subdirectories/files,
but this is probably what you want, anyway).

> find ! -name '.svn' -a -name '*.c' | xargs grep ...


find . -name '.svn' -prune -o -name '*.c' -exec grep whatever '{}' '+'
Reply With Quote
  #4  
Old 11-14-2008, 10:15 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: How to improve 'find' by skipping .svn directories?

On Nov 14, 5:12*am, wimpunk wrote:
> On 2008-11-14, PRC wrote:
>
>
>
>
>
> > Hi All,

>
> > I am developing with SVN and have to find some text in all *.c/*.h/*.S
> > files.

>
> > find ! -name '.svn' -a -name '*.c' | xargs grep ...
> > But the performance is not very good.
> > And I try
> > find ! -name '.svn'
> > the speed is not lower than the case where there are no any .svn
> > directories under my working directory.
> > Is there any option for `find' to improve performance?

>
> > Best Regards,
> > PRC
> > Nov 14, 2008

>
> I don't know the exact syntax but you have to tell find not to check
> subdirectories of .svn
> Your instruction isn't ignoring the .svn directories, it's just not
> displaying files named '.svn'- Hide quoted text -
>
> - Show quoted text -


Maybe something like this is more like what you really want:

find . -type d -a ! -name '.svn' | xargs grep whatever '{}/*.[xhS]'

i.e. use "find" to find all directories except the ".svn" ones, then
use grep on all *.c/*.h/*.S files in each directory. Check the
syntax...

Ed.
Reply With Quote
  #5  
Old 11-14-2008, 10:40 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: How to improve 'find' by skipping .svn directories?

2008-11-14, 06:15(-08), Ed Morton:
[[[.]
> Maybe something like this is more like what you really want:
>
> find . -type d -a ! -name '.svn' | xargs grep whatever '{}/*.[xhS]'



I don't know of any xargs implementation that would expand
wildcards.

> i.e. use "find" to find all directories except the ".svn" ones, then
> use grep on all *.c/*.h/*.S files in each directory. Check the
> syntax...

[...]

More like:

find . -name .svn -prune -o -type f -name '*.[xhS]' -exec \
grep whatever {} +

Or if you really want to use wildcards, for instance so that
files are sorted by names in every directory:

find . -type d ! -name .svn |
sed 's/./\\&/g;s|.*|grep whatever &/*.[xhS]|' | sh

(note that the obove skips dot files, but not dot directories).

With zsh:

setopt extendedglob
grep whatever (^.svn/)#*.[xhS](.)

(skips dot files and dot directories)

--
Stéphane
Reply With Quote
  #6  
Old 11-14-2008, 10:51 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: How to improve 'find' by skipping .svn directories?


I don't understand why not use -exec or -execdir action...

man find is written:
> -exec command {} +
> The command line is built in much the same way that
> xargs builds its command lines


....
?
Reply With Quote
  #7  
Old 11-14-2008, 11:44 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: How to improve 'find' by skipping .svn directories?

2008-11-14, 12:35(+00), Martin Vaeth:
> PRC wrote:
>>
>> I am developing with SVN and have to find some text in all *.c/*.h/*.S
>> files.

>
> I also do this regularly. I strongly recommend to use zsh as the
> interactive shell (it has _so_ many useful interactive features).
> For your task:
> grep whatever **/*.{c,h,S}


Note that brace expansion occurs before filename generation and
with zsh, if a pattern doesn't match, the command is aborted. So
the above is like writing:

grep whatever **/*.c **/*.h **/*.S

So we scan the directories 3 times, and if there are no .S
files, the command will be aborted even if there are .h or .c
files (unless you do a setopt CSH_NULL_GLOB or setopt
NO_NOMATCH).

So you may prefer:

grep whatever **/*.[chS]
or (with EXTENDED_GLOB):
grep whatever **/*.(c|h|S)

> (OK, this excludes even all ".*" subdirectories/files,
> but this is probably what you want, anyway).

[...]

And if not:

grep whatever (^.svn/)#*.[chS](D)

See also the grep -r --include=... --exclude=... of GNU grep.

--
Stéphane
Reply With Quote
  #8  
Old 11-14-2008, 04:32 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: How to improve 'find' by skipping .svn directories?

Stephane CHAZELAS wrote:
>
> grep whatever **/*.c **/*.h **/*.S
>
> So we scan the directories 3 times, and if there are no .S
> files, the command will be aborted even if there are .h or .c
> files (unless you do a setopt CSH_NULL_GLOB or setopt
> NO_NOMATCH).


You are right. I just never realized this, because I have nonomatch
turned on - I have worked with bash for years, and essential different
behavior would give me lots of bad surprises (like this one...).

> So you may prefer:
>
> grep whatever **/*.[chS]
> or (with EXTENDED_GLOB):
> grep whatever **/*.(c|h|S)


The former has the disadvantage that it cannot be generalized to the
important case **/*.(cc|h) but fortunately the latter can.
Thanks. So again I learnt something new.

> grep whatever (^.svn/)#*.[chS](D)


But admittedly this is not much shorter than the find command...
Reply With Quote
  #9  
Old 11-14-2008, 05:39 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: How to improve 'find' by skipping .svn directories?

On 14 Nov, 10:50, PRC wrote:

> I am developing with SVN and have to find some text in all *.c/*.h/*.S
> files.


> Is there any option for `find' to improve performance?


$ git svn clone $SVN_URL git-copy
$ cd git-copy
$ git grep -e 'some text' *.[chS]

This will improve many aspects of performance.
Reply With Quote
  #10  
Old 11-14-2008, 05:56 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: How to improve 'find' by skipping .svn directories?

Stephane CHAZELAS wrote:
> 2008-11-14, 06:15(-08), Ed Morton:
> [[[.]
>> Maybe something like this is more like what you really want:
>>
>> find . -type d -a ! -name '.svn' | xargs grep whatever '{}/*.[xhS]'

>
>
> I don't know of any xargs implementation that would expand
> wildcards.
> [...]
>
> Or if you really want to use wildcards, for instance so that
> files are sorted by names in every directory:
> [...]


Wouldn't something like this work?
find . -type d -a ! -name '.svn' | xargs sh -c 'grep whatever "{}/*.[xhS]"'

-Wayne
Reply With Quote
Reply

Thread Tools
Display Modes



All times are GMT -4. The time now is 06:00 AM.