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

Re: a grep question



begin  justin cunningham  quotation:

> Hi, I read through man on find and grep and am trying to search for an
> ip in some files contained in folders but every time I type in grep
> options it just hangs.  What am I doing wrong?

Hard to be sure, since you haven't shown us the command line you're
typing in that's giving you this problem.

Is grep perhaps sitting there waiting for data on stdin because you
didn't specify any files for it to search?

> Conversely i can go into /site.com/cgi-bin then cat any.cgi | grep
> 10.0.0.1 
> 
> and will get the desired result but instead of doing this for 'every
> file' in 'every folder' I want to search for the 10.ip in the files from
> the site's root directory.
> 
> Hope this is clear.

Not particularly clear, no.

In general, to search through files, you can use "find" piped into
"xargs grep". There is also "find . -exec grep {} ';'", but that's less
efficient since it will spawn grep once for each file, whereas xargs is
smart enough to spawn a minimal number of greps.

If you want to search only in a certain directory, not including
its subdirectories, then something like this should do the job:

 find /dir/to/search -maxdepth 1 -type f | xargs grep "pattern to search for"

You may also want to use find's -name option to limit the search to
files whose names match a particular pattern. When you do this, always
encase the name argument in single-quotes, e.g. -name '*.html' to search
all .html files.

When searching only a single directory, without its subdirectories, you
probably don't really need find; it would do as well in most cases just
to redirect grep's stderr to /dev/null, like this:

   grep "pattern to seach for" files 2>/dev/null

(assuming you are using a reasonable shell, i.e. not csh, which as I
recall cannot redirect stderr). This gets rid of grep's silly complaints
about "x is a directory" for every subdirectory.

If you want to search subdirectories as well, then leave out the
-maxdepth 1 clause.

Hope this helps.

Craig

Attachment: pgp9XutyrMtxY.pgp
Description: PGP signature


Reply to: