[plug] Limits of grep?

Bill Cullen billc at iinet.net.au
Tue Sep 26 21:26:21 WST 2000


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, I get the error message somthing to the effect,
> 
>         "File argument list too long"
> 
> What is the file argument limit for grep? I guess you need the grep source
> for this. I did not find any information in the man page.
> 
> Are there any other recommended tools to search through such large list of
> files?

This isn't a grep error, it's actually a shell error message.
You can get the same error by cd'ing to the directory and then
typing

    ls *

It's very important to understand that (unlike M$-DOS) the shell
exands wildcards and variables and then passes the expanded list
on to the program. For example, if I have a directory with the
files fred1 and fred2 and then I enter the command

    ls fred*

the ls program will actually receive two command line parameters:
fred1 and fred2. Similarly, if I have an environment variable
VAR1=fred and then do the command

    wc $VAR1

then the wc program will actually get passed the command line
parameter fred rather than $VAR1. 

There are several ways to get around the argument too long error:

  1. use the for statement as someone has suggested
  2. use the find statement as someone else has suggested
  3. try to break the list of files up a bit (ie a*)
  4. use the xargs command (which I think might be the best 
     solution in this case). ie. to list the names of all the
     files with the string "I'm a teapot"
   
         ls | xargs grep -l "I'm a teapot"

I'd take a look in my "UNIX Power Tools" book and be more specific
but unfortunately it's at work.
 
Good Luck,

Bill Cullen
-- 
---------------------------------------------------------------------
  Bill Cullen, ENFP, billc at wantree.com.au, Perth, Western Australia
               Life's too important to take seriously.
=====================================================================



More information about the plug mailing list