This brief guide explains how to fix the “/bin/rm: Argument list too long” error that sometimes pops up while trying to delete files or folders in Linux.
root@dwarf /var/spool/clientmqueue # rm spam-*
/bin/rm: Argument list too long.
Have you ever seen the above error in Linux? It happens when you have too many files in a directory and you are unable to delete them with a simple rm -rf *
I’ve run into this problem a number of times and after doing quite a bit of research online I came across a neat solution to work around this issue.
find . -name 'spam-*' | xargs rm
In the example above the command will forcefully delete all files in the current directory that begin with spam-. You can replace the spam-* with anything you like. You can also replace it with just a single * (asterisk) if you want to remove all of the files in the folder.
find . -name '*' | xargs rm
We have a more detailed guide on using the Linux find which you may find helpful and Xargs is Linux command that I wasn’t as familiar with. It makes passing a number of arguments to another command easier and I’m glad I’ve added it to my repertoire.