How to resolve the ‘/bin/rm: Argument list too long’ error

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.


If this article helped you, I'd be grateful if you could share it on your preferred social network - it helps me a lot. If you're feeling particularly generous, you could buy me a coffee and I'd be super grateful :)

buy a coffee for simplehelp.net


Home » Linux » How to resolve the ‘/bin/rm: Argument list too long’ error

7 thoughts on “How to resolve the ‘/bin/rm: Argument list too long’ error”

  1. this worked great. thank you. I had a crontab setup pulling pages for years with wget and i didnt realize that wget downloads and saves a file and i have a log of them, kept getting the argument list to long error and your solution worked. BTW i used curl now to pull web pages such as wordpress blogs, thank you for the help.

  2. LetsTalkTexasTurkeyHere

    I got this error from my RaspberryPi trying to erase a large amount of jpeg images from the current working directory.

    ls | grep “.jpg” | awk ‘{print “sudo rm “$0}’ | /bin/sh

    worked fine for me.

  3. Sorry for double post.

    Also, I get the same error I was trying to overcome using the plain rm command:

    find . -maxdepth 1 -type f -name ‘cache_*’ -print0 | xargs rm

    result:

    xargs: argument line too long

  4. Big thanks – find . -type f -print0 | xargs -0 /bin/rm saved the day for me with an overflowing pop acct

  5. Good catch using the print0 option, that’s an important one.

    Most find commands do not require the “-name” predicate. What’s usually more important is to make sure you’re deleting *files* and not something else you might not have intended. For this use “-type f” inplace of the “-name” option….

    find . -type f -print0 | xargs -0 /bin/rm

    A) Use the full path to the ‘rm’ command so your aliases don’t muck with things.
    B) Check your xargs command, you can sometimes, if needed, tell it to use one “result” at a time, such as (if you didn’t use print0 but regular print) “-l1”

  6. Safe variant for filenames with spaces, new lines and other whitespace characters:
    find . -name ‘*’ -print0 | xargs -o rm

Leave a Comment

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