rm or find on Linux: "Argument list too long"

So you're try­ing to delete a huge list of files and see­ing this error. It's a lim­i­ta­tion of your ker­nel, because your OS can­not han­dle too many files at once.

The solu­tion is sim­ple. Use the fol­low­ing command:

find . -name 'dos-*' -print0 | xargs -0 rm

Where "dos-" is a string con­tained in the files you wish to delete. That should do it.

Sug­ges­tion: for log fold­ers that fill up quickly, e.g., the logs of a data­base or Apache mod­ules such as mod_evasive, it's usu­ally a good idea to auto­mat­i­cally remove the files older than a cer­tain num­ber of days. For instance, to remove all log files in the mod_evasive folder older than 2 days, use this com­mand in the crontab:

01 01 * * * find /var/log/apache/mod_evasive/* -mtime 2 -exec rm {} \; > /dev/null 2>&1