How to avoid the “grep: Argument list too long” error

bashgrepscriptxargs

I'm using Mac 10.7.5 and on a bash shell. I'm trying to find instances of a string in a group of files but keep getting this error

Daves-MacBook-Pro:folder davea$ find . -name "*" | xargs grep 'state-icons'
xargs: grep: Argument list too long

How can I run the command (or a similar one) to avoid this error?

Best Answer

You can use the -n option of xargs to limit the number of arguments.

find . -name "*" | xargs -n 20 grep 'state-icons'

But a better solution is to use -type f instead of -name "*" in order to limit the list to ordinary files, avoiding the error message for each folder.

Note that it does not work for files with whitespace in their names.