Recursive grep

Recursive grep

Post by Ralph Finc » Sat, 18 Oct 1997 04:00:00




> Does anybody know how to do a recursive grep?

You mean, how to grep for files recursively into directories?

Here's one way.

% find . -name \* -type f -print | xargs grep <string>

--
Ralph Finch, Senior Engineer, W.R.      Tel.    916-653-8268
Delta Modeling Section                  Fax.    916-653-6077

1416 Ninth St, Sacramento, CA 95814     finger for PGP public key

 
 
 

Recursive grep

Post by Dan Riede » Sat, 18 Oct 1997 04:00:00



> Does anybody know how to do a recursive grep?

> and, How to disable the "don't have permission" error mesage?

> Thanks in advance.

A little more detail on the real problem is required here.  Recursive
grep should work.

  # find / -print |grep <something> |grep <something_else> |grep
<something_else_again>

to "disable" error mesages redirect stderr to /dev/null

  # find / -print |grep <something> |grep <something_else>  2>/dev/null

Dan

 
 
 

Recursive grep

Post by Guy Harr » Sat, 18 Oct 1997 04:00:00



Quote:>% find . -name \* -type f -print | xargs grep <string>

And, if you want to have it tell you the names of the files in which it
found the strings:

        % find . -name \* -type f -print | xargs grep <string> /dev/null

("grep" normally gives the file name only if told to scan more than one
file; "/dev/null" contains nothing, so it won't find anything in that
file, but that'll make sure it always has more than one file to scan.)

Also, the "-name \*" is unnecessary, unless you don't want it to search
through files with "." as the first character; if you want it to search
*all* files, including ".XXX" files, do

        % find . -type f -print | xargs grep <string> /dev/null

You can, of course, use a pattern other than "*", or even multiple
patterns, e.g.

        find . \( -name '*.[chylsSxm]' -o -name '*.ch' -o -name '*.fth' \
            -o -name '*.sh' -name '*.m[td]' -o -name '*.asm' \) -print | \
            xargs grep <string> /dev/null

(the "find" comes from a script of mine that I use to search through
source files).
--
Reply, or follow up, but don't do both, please.



 
 
 

Recursive grep

Post by OSC » Sat, 18 Oct 1997 04:00:00


Does anybody know how to do a recursive grep?

and, How to disable the "don't have permission" error mesage?

Thanks in advance.

 
 
 

Recursive grep

Post by Bruce Fountai » Sun, 19 Oct 1997 04:00:00




> > Does anybody know how to do a recursive grep?

> You mean, how to grep for files recursively into directories?

> Here's one way.

> % find . -name \* -type f -print | xargs grep <string>

Using "-name \*" is redundant, and using xargs can be
dangerous for large file lists. I prefer:

  find . -type f -exec grep "searchpattern" {} /dev/null \;

The "/dev/null" is added as a dummy argument to encourage
grep to print the filename (normally grep does not print the
filename if only one file is being grepped). Avoid grepping
binary files if possible.

--

Regards,

Bruce

+--------------------------------------------------------------------------+
| Bruce Fountain                 NEC Australia - New Product Development   |

+--------------------------------------------------------------------------+

 
 
 

Recursive grep

Post by Brian Hanl » Sun, 19 Oct 1997 04:00:00


A couple of notes: if you are only interested in the files in which
a pattern occurs, and not the actual line, you might try

grep -l pattern file1 (that's -L in lowercase)

instead of
grep pattern file1 /dev/null

It's much cleaner.

Also, if you do want the actual lines printed:
Insofar as doing a grep on possibly-binary files is concerned, I usually
pipe the grep results through 'cat -v' ... this simply means print a
visible representation of non-printing characters. Useless use of cat?
Maybe, but it will avoid screwing up your terminal.

Cheers,
Brian

--
Brian S Hanley                   | "Fear is the main source of superstition,
1B/2A Systems Design Engineering |  and one of the main sources of cruelty.
U of Waterloo                    |  To conquer fear is the beginning of wisdom."
(On workterm at Telesat Canada)  |  - Bertrand Russell

 
 
 

Recursive grep

Post by Bruce Fountai » Tue, 21 Oct 1997 04:00:00



> Why do you think xargs is dangerous? The purpose of xargs is to
> be used with file lists that breaks the command line length limit.

You are right and I am wrong. xargs will cache the arguments until
a buffer fills, then invoke the command with the cached args, then
start accumulating args again

Quote:> >    I prefer:
> >   find . -type f -exec grep "searchpattern" {} /dev/null \;

> This will be slow, much slower than the version using xargs.

It will be slower, but more even since you don't have to wait for the
buffer to fill. Also, you can be sure that grep will never be called
with only one argument (mind you, you could have done that with
xargs too).

Just splitting hairs really. mea culpa.
--

Regards,

Bruce

+--------------------------------------------------------------------------+
| Bruce Fountain                 NEC Australia - New Product Development   |

+--------------------------------------------------------------------------+

 
 
 

Recursive grep

Post by Dale Talco » Tue, 21 Oct 1997 04:00:00






>> > % find . -name \* -type f -print | xargs grep <string>
>> ... and using xargs can be
>> dangerous for large file lists.
>Why do you think xargs is dangerous? The purpose of xargs is to
>be used with file lists that breaks the command line length limit.

If you control the filenames, xargs is fine.  If other people create
the files, then you should not use xargs.  The problem is that someone
can create files with embedded newlines.  The newlines confuse xargs
into thinking the one name is actually two names (what precedes the
newline and what follows).  For the case with grep, this might not be a
problem, but it is generally a security risk.

For example, suppose you used the following script to clean out /tmp

        cd /tmp
        find . -type f -mtime +14 -print | xargs rm -fr --

Now, suppose someone does

        mkdir -p '/tmp/foo
        '
        touch '/tmp/foo
        /home'

Guess what disappears the next time root runs the /tmp cleaning script?

However, if you -exec rm directly from the find, rm sees the correct name
and does what it should.

--

 
 
 

Recursive grep

Post by Paul D. Smi » Tue, 21 Oct 1997 04:00:00


Of course, you can also avoid this problem without the efficiency hit by
using GNU find (with -print0) and GNU xargs (with --null or -0).

FWIW.

--
-------------------------------------------------------------------------------

 "Please remain calm...I may be mad, but I am a professional." --Mad Scientist
-------------------------------------------------------------------------------
     These are my opinions--Bay Networks takes no responsibility for them.

 
 
 

Recursive grep

Post by John R MacMill » Tue, 21 Oct 1997 04:00:00


|...  For the case with grep, this might not be a
|problem, but it is generally a security risk.

Some versions of find/xargs support a NUL terminator to deal with
this, and in cases that don't, you can often filter the results from
find to sanity check them.  For example, change:

|       cd /tmp
|       find . -type f -mtime +14 -print | xargs rm -fr --

to:

        find /tmp -type f -mtime +14 -print | grep '^/tmp/' |
                xargs rm -fr --

This example would still leave * files around, and could delete a
young file from /tmp, but with more work, it's possible to deal with
them too; I'm just trying to illustrate the general idea.

|However, if you -exec rm directly from the find, rm sees the correct name
|and does what it should.

Of course where the individual execs aren't a problem (who really
cares if your nightly /tmp cleanup takes 10 seconds or 10 minutes?),
this is the best solution.
--
To reply by mail, please remove "mail." from my address -- but please
send e-mail or post, not both

 
 
 

Recursive grep

Post by Mike Ekhol » Wed, 22 Oct 1997 04:00:00



: Does anybody know how to do a recursive grep?

try 'grep -v'

-Mike
--

           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 
 
 

Recursive grep

Post by Lars Gregersen (c9082 » Wed, 22 Oct 1997 04:00:00


: Does anybody know how to do a recursive grep?

I've been waiting for that question. I've programmed a small utility in
perl that can grep recursively. It was written because I though that the
syntax of find was to complicated and impossible to remember.

My programme "findgrep" works just like grep, but with perl regular expressions.
It even uses regular expressions on the file names. You must have Perl
installed in your computer to use it.

You can download the programme at:
http://www.gbar.dtu.dk/~matlg/findgrep/

Findgrep is free!

Let me know what you think of it there is still room for changes. Comments
are wellcome.

-------------------------------------------
Lars Gregersen
Department of Chemical Engineering
Technical University of Denmark


Homepage: http://www.gbar.dtu.dk/~matlg/

 
 
 

1. Non-recursive Recursive Grep? Apology!!!

Sorry everyone!!! It was my fault, I was executing the command:

rgrep <item> *

Which was wrong. I (accidentally) entered the command without the * on the end
and it worked fine. DOH!

Al

--
Al Priest, Software Engineering, CCI GmbH, Germany. Phone: +49(0)5931 805463
These views are mine, all MINE!! Whoa ha ha. (not CCI's).

2. Swing1.0 and SCO

3. Non-recursive Recursive Grep?

4. SuSE Linux 6.1 on home built pc

5. recursive grep

6. Trident 9440 setup in 1024x768 mode

7. sed - executing multiple commands, a phrase as a single expression

8. Recursive Grep

9. recursive grep

10. A recursive grep? Need a GOOD searcher.

11. recursive grep

12. How to use xargs for recursive grep?