-bash: /bin/grep: Argument list too long

$ grep ‘some phrase’ * | wc

Solutions:

1. The general solution is to use ‘xargs’.

2. If you’re a Linux masochist you could also try rebuild the kernel with a change to:

include/linux/binfmts.h

:

#define MAX_ARG_PAGES 32

Perhaps make it 64? Or more?

 

3. Use find

find . -type f -exec grep ‘some phrase’ {} + | wc

E.g.

Using -exec efficiently:

The -exec option to find is great, but since it runs the command listed for every found file, it isn’t very efficient.  On a large system this makes a difference!  One solution is to combine find with xargs as discussed above:

find whatever… | xargs command

However this approach has two limitations.  Firstly not all commands accept the list of files at the end of the command.  A good example is cp:

find . -name \*.txt | xargs cp /tmp  # This won’t work!

(Note the Gnu version of cp has a non-POSIX option -t for this.)

Secondly filenames may contain spaces or newlines, which would confuse the command used with xargs.  (Again Gnu tools have options for that, find … -print0 |xargs -0 ….)

There are POSIX (but non-obvious) solutions to both problems.  An alternate form of -exec ends with a plus-sign, not a semi-colon.  This form collects the filenames into groups or sets, and runs the command once per set.  (This is exactly what xargs does, to prevent argument lists from becoming too long for the system to handle.)  In this form the {} argument expands to the set of filenames.  For example:

find / -name core -exec /bin/rm -f ‘{}’ +

For more info take a look here:

http://find.unixpin.com/ldocs/FindCmd.htm

Leave a Reply

Your email address will not be published. Required fields are marked *