[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Search for string in files



On Sun, Aug 26, 2007 at 07:46:01AM +0200, Jonathan Kaye wrote:
> Johannes Tax wrote:
> 
> > Hi,
> > 
> > I'm trying to figure out how to find a certain string inside a bunch of
> > files. If I, for examples, look for a certain function in a large source
> > tree, I could do
> > 
> > cat `find . -name '*.c'` | grep 'a_certain_function'
> > 
> > but this seems quite awkward, furthermore it doesn't help that much
> > because I don't know in which file the string was found. Maybe there's a
> > tool that makes it possible to find a string in a bunch of files and
> > also to list in which file the string was found? Or any modification to
> > the command given above?
> > 
> > Thanks a lot in advance,
> > 
> > Johannes
> > 
> > --
> > Johannes Tax
> > jo.ey@gmx.at
> Hi Johannes,
> If you don't mind a non-cli-solution you can use the Find File built into
> Konqueror. It's in the Tools menu. You just specify your filter and then go
> into the Contents tab where you can specify which text your looking for.
> You get the results in a nice clickable pane. Maybe other file managers
> have a similar feature.

And if you're interested in sticking with the command line, the invocation
you probably want is:

find . -name '*.c' | xargs grep 'a_certain_function'

The xargs command is almost essential for this sort of activity:  It
takes its standard input and uses it as additional arguments to the
command.  It also avoids various limits as to the length of argument
lists.  The version with 'cat' above could well fail if you have too
many findable .c files.

The find above takes advantage of grep's default of listing filenames in
matches if there's more than one on the command lines.  I'm more likely
to use a variant like:

find . -type f | xargs grep -li pattern

That'll search all ordinary files, case insensitive, and only give the
names of the matching files.  The man pages for find and grep can be
very helpful for fine-tuning this kind of search.

Jon Leonard



Reply to: