grep: subdirectories

grep: subdirectories

Post by Matthew Robins » Wed, 25 Jan 1995 09:52:09



Quote:>find . -type f -name "*" -print | xargs "grep" "search string"

>...does not exhibit this behaviour.

I would be wary using shell metacharacters ("*") in any command that requires
all names in a current directory.  In some cases of very large directories, it
may cause the shell to expand the line to something too large for UNIX to run.

Use:

find . -type f -exec grep -l '<string>' {} \;

------------------------------------------------------------------------------
Matthew Robinson                                                         "OT!"
Senior Customer Service Rep.

                  "Never,.. NEVER... quit" (Winston Churchill)
---------------------------------o O O O o------------------------------------

 
 
 

grep: subdirectories

Post by Dave Carrig » Thu, 26 Jan 1995 01:28:06


 >> find . -type f -name "*" -print | xargs "grep" "search string"
 >>
 >> ...does not exhibit this behaviour.

 Matthew> I would be wary using shell metacharacters ("*") in any
 Matthew> command that requires all names in a current directory.  In
 Matthew> some cases of very large directories, it may cause the shell
 Matthew> to expand the line to something too large for UNIX to run.

But the shell isn't expanding the metacharacter, because it is in
quotes. And the purpose of xargs is to ensure that the command line
never gets too big. The find command above works almost perfectly.

The only problem would be if xargs ended up splitting up its input
into more than one batch of files, and the second batch had a single
file. Then, if grep matched a string in that single file, you wouldn't
know which file had the match. Of course, this exception is a little
pathological...

 Matthew> Use:
 Matthew> find . -type f -exec grep -l '<string>' {} \;

This is OK, but slow, especially if there are a lot of files, because
you have to exec grep once for each file. Also, if the original poster
wants to see the pattern that matched, then grep -l won't do it. Of
course, without the -l, you won't see which filename contained the
match. A slightly better solution is to force grep to print the
filename and the match by passing two files to grep each time:

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

Cheers,

                                ^

Dave Carrigan                  /|\            Northern Forestry Centre
Systems/Network Administrator   |     Unix/VMS/TCP-IP/DECnet/Pathworks

 
 
 

1. grep: subdirectories


In `sh' do:

-------------------------------------------------------------------------------
tmpf="/tmp/tmp.$$"

# Scan all files
find . -type f -print | \
while read file; do
  echo '\nFILE: '$file >>$tmpf
  grep STRING $file >>$tmpf
done

# Remove files with no reference
sed \
  -e '/^$/N' -e ':nx' -e '/^\nFILE:/N' -e '/^\nFILE:.*\n$/{' \
  -e D -e D -e N -e 'b nx' -e '}' <$tmpf

rm -f $tmpf
-------------------------------------------------------------------------------

--

Services Informatiques, Universite de Montreal.
C.P. 6128, Succursale centre-ville, Montreal,            
Quebec, Canada.   H3C 3J7                      

2. Where to post requests for project help

3. How to grep subdirectories?

4. **HELP!** Problem with HP ScanJet 4c

5. grep does NOT scan subdirectories, using "-r" option ?

6. icq

7. grep into subdirectories

8. newbie

9. ? grep recursively down all subdirectories

10. how can i user grep in subdirectories recursively?

11. Need a grep that recurses subdirectories

12. How to grep all the subdirectories?

13. How to grep in subdirectories