Bash: Grep through a large number of files – argument list too long

The other day I was searching for a particular email file in an Maildir on one of our mail servers. Knowing only the From address, I thought I’d use grep. Unfortunately it failed and gave me an error:/bin/grep: Argument list too long. Here is how to overcome this grep error…

GNU Bash Logo

/bin/grep: Argument list too long

How to grep through a large number of files? For example:

grep -r "example\.com" *
-bash: /bin/grep: Argument list too long

Well, this was no wonder, since there are over 190500 files in that particular Maildir:

ls -1 | wc -l
190516

There are several ways of overcoming this error message. One, for instance is:

find . -type f | xargs grep "example\.com"

A second option to overcome the error is: substitute the “*” (asterisk) with a “.” (dot), like:

grep -r "example\.com" .

In newer versions of grep you can omit the “.“, as the current directory is implied.

Protip: did you know you use grep on Windows? This comes in handy for forensic log parsing and analysis on Windows Server IIS.


Did this post helped you solve a problem? Or dit you find it interesting? Support Sysadmins of the North with a direct donation via Paypal of by bank wire transfer IBAN: NL31 ABNA 0432217258 (Jan Reilink). Just $ 5,- or € 5,- is more than enough, thanks!

Support SAOTN.ORG


8,480 views

2 comments

Leave a Reply

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