How can I avoid the "grep: Argument list too long" error?
By Emma Valentine •
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 longHow can I run the command (or a similar one) to avoid this error?
12 Answers
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.
7What about this if you want to seach through the filename:
for x in ./**/*.*; do echo "$x" | grep 'state-icons' ; doneand this if you want to seach through the file content:
for x in ./**/*.*; do grep 'state-icons' "$x" ; done 3