[Novalug] bash commands

Joel Fouse joel at fouse.net
Fri Jan 18 15:02:43 EST 2008


On Fri, 2008-01-18 at 19:53 +0000, Jeff Stoner wrote:

> On Fri, 18 Jan 2008, Nino Pereira wrote:
> 
> > I'd like to search all the files in a directory
> > for the occurrence of a particular
> > string. It must be something like
> >
> > do (until you have had all the files)
> >  cat *.f | grep --with-filename 'string'
> > od
> >
> > but I don't have an example to modify from. Can you point me
> > to one?
> 
> Got GNU grep?
> 
> grep -r 'string' *.f
> 
> 
> --Jeff


Some grep options I use on a regular basis:

-i  ...  search case-insensitively
-r  ...  search recursively (in the example above, you wouldn't even
need the -r if all the files are in the same folder)
-l   ...  show only the filenames that contain matches rather than
displaying each matching line
-c  ...  show all filenames scanned with the number of matching lines
contained in each (including zero)

As Jeff pointed out, the file parameter isn't limited to a single name;
rather, you can use all manner of what they call shell globbing, because
the bash shell converts the globs to concrete filenames before passing
them on to grep as distinct parameters:

grep 'stringToFind' *.[ch]    // search for term in all files ending in
".c" or ".h"

You can also use multiple filename arguments:

grep -r 'stringToFind' ./subDir1/*.[ch] ./subDir3/*.txt ./subDir5

Grep is your friend.  It's very rare that I need to use some other
utility in conjunction with it (cat, find, etc.).

- Joel

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://calypso.tux.org/pipermail/novalug/attachments/20080118/3ac9e988/attachment.htm


More information about the Novalug mailing list