Date: Thu, 27 Dec 2001 19:09:41 -0500 From: Simon Morton <simon.morton@verizon.net> To: Drew Tomlinson <drew@mykitchentable.net> Cc: Joe Clarke <marcus@marcuscom.com>, questions@FreeBSD.ORG Subject: Re: How To Recursively Search Directory For Text String In Files? Message-ID: <3C2BB845.7070508@verizon.net> References: <20011227172121.Q11529-100000@shumai.marcuscom.com> <014501c18f26$9d430970$c42a6ba5@lc.ca.gov>
next in thread | previous in thread | raw e-mail | index | archive | help
That won't quite do it if you want only the names of matching files.
The following will do it for you without xargs:
find <dir> -type f [-name <file name pattern] -exec grep -l <pattern> {} \;
which is almost the same as
find <dir> -type f [-name <file name pattern] -exec grep -q <pattern> {}
\; -print
grep -l prints out just the name of any file containing the pattern.
grep -q prints nothing but returns 0 for matching files
-exec is a conditional operator; if the command called by -exec returns
0, -exec returns TRUE. This can be useful if you wanted an action
other than -print, say -delete for example.
HTH
Simon
Drew Tomlinson wrote:
>>>
>>You can do grep -r <pattern> *, but I prefer:
>>
>>find <directory> -type f -name <optional filename pattern> | xargs
>>
> grep
>
>><pattern>
>>
>
> Thank you very much! I *knew* it had to be possible but had not come
> across xargs yet. It looks like a very powerful tool that will have a
> lot of uses!
>
> Drew
>
>
>>For example:
>>
>>find /usr/ports -type f -name Makefile | xargs grep -i snmp
>>
>>This would search all Makefiles found anywhere in the /usr/ports tree
>>
> and
>
>>grep case-insensitvely through them for the pattern "snmp".
>>
>>Joe
>>
>>
>>>Thanks,
>>>
>>>Drew
>>>
>>>
>>>To Unsubscribe: send mail to majordomo@FreeBSD.org
>>>with "unsubscribe freebsd-questions" in the body of the message
>>>
>>>
>>>
>>
>>To Unsubscribe: send mail to majordomo@FreeBSD.org
>>with "unsubscribe freebsd-questions" in the body of the message
>>
>>
>>
>
>
> To Unsubscribe: send mail to majordomo@FreeBSD.org
> with "unsubscribe freebsd-questions" in the body of the message
>
>
--
http://www.SimonMorton.com
smorton at acm dot org
\rm -rf /bin/laden
To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?3C2BB845.7070508>
