Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 27 Dec 2001 17:23:33 -0500 (EST)
From:      Joe Clarke <marcus@marcuscom.com>
To:        Drew Tomlinson <drew@mykitchentable.net>
Cc:        questions@FreeBSD.ORG
Subject:   Re: How To Recursively Search Directory For Text String In Files?
Message-ID:  <20011227172121.Q11529-100000@shumai.marcuscom.com>
In-Reply-To: <011701c18f24$951d3b00$c42a6ba5@lc.ca.gov>

next in thread | previous in thread | raw e-mail | index | archive | help


On Thu, 27 Dec 2001, Drew Tomlinson wrote:

> OK, I am beginning to understand the power of FBSD and am sure this is
> possible.  I just don't know how to do it.  What I want to do is search
> all files in my current directory and all the directories below it for a
> text string and then know what file(s) contains the string.  I
> understand that grep will do the search but my knowledge is limited to
> "cat file.txt | grep string".  How can I construct a command in tcsh to
> feed each file to cat and then feed it to grep *AND* know the name of
> the file grep found the match?  Do I have the right concept?  Is there a
> better way to accomplish my goal?
>

You can do grep -r <pattern> *, but I prefer:

find <directory> -type f -name <optional filename pattern> | xargs grep
<pattern>

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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20011227172121.Q11529-100000>