/bin/rm: Argument list too long

My antispam system is configured to classify incoming messages and copy them to a particular sub-directory. As time pass more a more messages are accumulated in this folder. Recently I tried to eliminate old messages and I found myself with this:

/bin/rm: Argument list too long

And then I took a look at the directory and saw something like this:

ls -al | wc -l
58259

It seems that the rm command can’t deal with such number of arguments. Fortunately there are some workarounds for this problem.

ls | xargs rm

Or you can combine rm with find:

find . | xargs rm

2 Responses to “/bin/rm: Argument list too long”

  1. anonymous says:

    It seems that this is a limit inside the kernel and not a limitation of rm or a limitation of the shell. On the other hand if the filenames have white spaces the best way is to do something like this:

    find . -name file_names_expr -print0 | xargs -0 rm

    Morpheo

  2. anonymous says:

    Since the pruning of overcrowded quarantine directories is a pretty much recurring task, I have a handy shell function to do that (incorporates a basic sanity check, skips directories and dotfiles):

    wipe() {
    if [ "${1}" == "" ] ; then
    echo “Wiping out all non-directory files in ${PWD}.”
    else
    echo “Wiping out all files starting with ‘${1}’ in ${PWD}.”
    fi

    echo -n ‘Type “YES” to confirm: ‘
    read YESNO
    if [ "${YESNO}" != "YES" ] ; then
    echo ‘Maybe next time.’
    return 0
    fi

    # ok, wipe it
    find . \( -name “${1}*” -and \! -name “.*” -and -type f \) -print0 | xargs -0 rm
    }

    Hope it helps.

    Kelas.

Leave a Reply

You must be logged in to post a comment.