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

root@dwarf /var/spool/clientmqueue # rm spam-*
/bin/rm: Argument list too long.

Ever seen this error in Linux when you have too many files in a directory and you are unable to delete them with a simple rm -rf *? I have run into this problem a number of times. After doing a bit of research online I came across a neat solution to work around this issue.

find . -name 'spam-*' | xargs rm

In the above instance 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 * if you want to remove all files in the folder.

find . -name '*' | xargs rm

We have covered the Linux find command in great detail earlier. Xargs is Linux command that makes passing a number of arguments to a command easier.

{ 13 comments… add one }
  • Aleš Friedl February 19, 2009, 7:16 am

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

  • MikeT February 19, 2009, 6:30 pm

    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”

  • myhnet February 25, 2009, 12:12 am

    find . -type f -exec rm {} \;

  • Thomas Hutton March 26, 2010, 6:35 pm

    Thanks, saved my rear!

  • Michael August 21, 2010, 10:32 am

    Thanks for your help!

    But what if you have a dash (-) in the file names? It causes the output to say rm: missing operand. (there are too many files to delete without using the find command method).

  • Kevin Polley October 5, 2010, 12:34 pm

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

  • redesigned March 12, 2011, 7:42 pm

    ls | xargs rm

    (works even for those shared hosts that block access to find, like nexus)

  • из консоли ls -l /usr/local/vpopmail/domains/oldcomp.ru/abuse/Maildir/cur/ | awk ‘{print “rm /usr/local/vpopmail/domains/oldcomp.ru/abuse/Maildir/cur/”$9}’ | /bin/sh

    удаляет все !!!

  • Francesco August 4, 2011, 4:34 am

    After running find . -name ‘*’ -print0 | xargs -o rm

    xargs: invalid option — o

  • Francesco August 4, 2011, 4:36 am

    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

  • LetsTalkTexasTurkeyHere January 7, 2015, 8:03 pm

    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.

  • Darrell Miller September 11, 2017, 2:29 pm

    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.

Leave a Comment