[ILUG] Limits of grep?

Brady, Padraig Padraig.Brady at compaq.com
Thu Sep 28 11:42:02 IST 2000


> -----Original Message-----
> From: Fergal Daly [mailto:fergal at esatclear.ie]
> Sent: 27 September 2000 22:23
> To: Paul Kelly; Subba Rao
> Cc: Irish Linux Users
> 
> At 12:00 26/09/00, Paul Kelly wrote:
> >Subba Rao wrote:
> > > I have a directory of 10000+ text files and would like to 
> search for
> > > some strings in these files. When I tried using "grep" 
> command with an
> > > asterisk...
> >
> >That's a shell issue as much as grep. Try find . -print | xargs grep
> >"pattern"
> 
> This will go wrong on any file that has a space or a shell 
> meta character and may even end abruptly. To avoid this use
> 
> find . -print0 | xargs -0 grep "pattern"
> 
> find will output the filenames in null separated format and 
> xargs will expect them like that,
> 
> Fergal

I find the following construction more general,
in dealing with spaces and tabs in filenames.
This would allow you to pass the output to 
something other than xargs, for e.g. to sort
files by size you could do:

find . -type f -printf "%p\0%s\n" | # %p = filename(with spaces) followed
tr ' \t\0' '\0\1 ' | #remove spaces, tabs in file names
sort +1n +0 | #sort by filesize & then by filename
tr '\0\1' ' \t' #reset any space & tabs in filenames

I find the -printf option to find really useful.

Padraig.




More information about the ILUG mailing list