/bin/rm: Argument list too long

Part of my job is to make sure our servers run as smoothly as possible during my long and arduous graveyard shift. At times it can be a bit slow but once in a while a task comes up thats rather intriguing. One such night was last Thursday night. I wanted to remove the entire contents of the /var/spool/mqueue/ directory but couldn’t. When I attempted to remove the files, I was surprised to see the error message:

[root@host /var/spool/mqueue] # rm ??*
/bin/rm: Argument list too long.

It’s not like I have never seen that error before but it made me feel like saying, “WTF!”

Why was I not allowed to remove the contents of the mqueue directory when /bin/rm has worked for me so many times before? Honestly, I don’t know. I am going to assume that there is some type of kernel level limit somewhere that /bin/rm knows about and I don’t, 😉

Ultimately, I overcame this minor setback with the following command:

find . -type f -name '??*' -print -exec rm -f {} \;

The ‘??’ options to -name are shell regular expression meta characters. Meaning, that the preceding item is optional and will be matched, at most, once. Because I don’t want the risk of removing the “dot” directories (‘.’ and ‘..’), I added two regular expressions assuring that two or more characters need to match before the delete action takes place. I’m sure you can figure out the rest.

This is all fine and dandy but what if you have spaces in the filenames? Using the command above, not all files with spaces in the name will be removed. So what to do? In comes xargs.

find . -type f -name '??*' -print0 | xargs -0 rm -f

You need to use print’s “-0” option in conjunction with xargs “-0” option otherwise you will have adverse affects since the shell will think the spaces are new arguments when in reality its just a single file that happens to have a space in the name itself.

A combination of find and xargs is the way to go if you don’t wish to worry about what the file is called.


About this entry