Operating System - HP-UX
1752636 Members
6203 Online
108788 Solutions
New Discussion юеВ

Re: Excluding a directory from a recursive search in ksh

 
SOLVED
Go to solution
vinay naik
Occasional Contributor

Excluding a directory from a recursive search in ksh

Hello all,
I would like to avoid seaching through the .svn directory whenever I grep for a pattern.
For ksh, I generally use
find . -name "*" -exec grep {} \; -print

How do I "exclude" a particular directory from a recursive search ?

Thanks..
9 REPLIES 9
James R. Ferguson
Acclaimed Contributor

Re: Excluding a directory from a recursive search in ksh

Hi:

One way:

# find . ! -path "./.svn/*" -type f -exec grep {} +

Regards!

...JRF...
vinay naik
Occasional Contributor

Re: Excluding a directory from a recursive search in ksh

Thanks James.
But I have multiple .svn in recursive directories. The solution excludes .svn only in the current directory.
James R. Ferguson
Acclaimed Contributor
Solution

Re: Excluding a directory from a recursive search in ksh

Hi (again):

> But I have multiple .svn in recursive directories. The solution excludes .svn only in the current directory.

You didn't make that clear at the oneset, so try this:

# find . ! -path "*/.svn/*" -type f -exec grep {} +

Regards!

...JRF...
vinay naik
Occasional Contributor

Re: Excluding a directory from a recursive search in ksh

That worked fine. Thanks for your help.
vinay naik
Occasional Contributor

Re: Excluding a directory from a recursive search in ksh

Hi,
One more question..
How can I 'avoid' searching a pattern in binary files such as executables and libraries? Are there options to the 'find' or 'grep' commands to isolate search only in ASCII files?

-Thanks
Mel Burslan
Honored Contributor

Re: Excluding a directory from a recursive search in ksh

find or grep dors not have switches to differentiate between ascii and binary files. you need to build a logic like :

for f `ls /tmp`
do
file ${$f} | grep -q text
r=${?}
if [ ${r} -eq 0 ]
then
#this is a text file either command script or ascii text
grep ${f
fi
done

HTH
________________________________
UNIX because I majored in cryptology...
James R. Ferguson
Acclaimed Contributor

Re: Excluding a directory from a recursive search in ksh

Hi (again):

> How can I 'avoid' searching a pattern in binary files such as executables and libraries?

We could leverage Perl, like:

# perl -MFile::Find -e 'find(sub{push @f,$File::Find::name if $File::Find::dir !~m{\.svn/} && -f $_ && -T _},".") ;@a=`grep -i $ARGV[0] @f`;print for sort @a' PATTERN

Specify your PATTERN to be matched as the argument to the script. Note that the '.svn' directory has its dot character escaped (backslashed) to signify that the dot is not a metacharcter in the regular expression match.

This script looks for files in the current directory but skips any that are not "text" ('-T') ones.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Excluding a directory from a recursive search in ksh

>I would like to avoid searching through the .svn directory whenever I grep for a pattern.

If you want to exclude all .svn directories and subdirectories, then the right option is -prune. Especially if you don't want find to search those subdirectories.

To use -prune:
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1276654
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1309998
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1323863
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1271016
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1303248
Daavid Turnbull_1
New Member

Re: Excluding a directory from a recursive search in ksh

Another approach, probably not one for the purists but one I find syntactically easier is to use xargs and grep.

find . | grep -v svn | xargs grep

I like it because you can iteratively tune the list of files with multiple "| grep -v pattern" until you get what you want and then pipe to xargs and grep for the pattern within the files you are looking for.

(I have never liked the -exec find syntax ;-)