Parameter List Too Long

If your are “fortunate” enough to have to delete a lot of file from a directory on a Unix system, you are likely to encounter the “Parameter List Too Long” problem. This problem is caused by Unix trying to replace the “*” you just used by all names in the directory. This list is then passed to the rm command, which complains that it can not handle that much parameters.

To get around this limitation, you have a couple of options:

Delete all files in the current directory
By far the fastest and most compatible way to delete all files in the current directory without recursing into subdirectories is:
ls | xargs rm
This is the fastest command, because xargs makes sure the maximum allowed number of parameters is passed to rm, resulting in fewer calls/startups to rm.

Delete all files in the current directory and all subdirectories
To delete files not only from the current, but also from all subdirectories, use
find . -name "*" -exec rm -rf {} \;
This is somewhat slower, since rm is executed seperately for each file.

Above commands are tested on AIX and Linux.

Advertisements

One Response to Parameter List Too Long

  1. rolfje says:

    Original reply by Sybren:

    That last command doesn’t only remove files from the current dir and subdirs, it also deletes the subdirs themselves. Just be warned 😉

    If you want to remove files only and leave the subdirectories in place, use:
    find . -type f -exec rm {} \;

    None of those commands work when you have files with spaces in them. In that case, use the “-print0” option of find combined with the “-0” option of xargs. I haven’t tested this on AIX though, only on GNU find/xargs as shipped with most Linux distributions.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s