Skip to content


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

Linux

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.

Posted in Linux.

Get Simple Help tutorials just like this one in your email inbox every day - for free! Just enter your email address below:

 

You can always opt out of this email subscription at any time.

3 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Aleš Friedl says

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

  2. MikeT says

    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″

  3. myhnet says

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



Some HTML is OK

or, reply to this post via trackback.