Removing thousands of files using rm
wmoran at potentialtech.com
Tue Jul 6 08:13:40 PDT 2004
"Steve Bertrand" <iaccounts at ibctech.ca> wrote:
> I often have the need to remove hundreds or even thousands of files from a
> single directory (very often). Using rm, I usually get:
> pearl# rm -rvf *
> /bin/rm: Argument list too long.
> Is there any way to work around this instead of having to select a small
> bunch of files at a time to remove?
> The directory I am trying to remove the files from is always hot, so
> deleting the directory is unfortunately not an option. (I don't think).
I think the canonical answer is to use find with the -delete option. Like
find /dir -delete
The longer answer is that the reason it fails is because the shell expands
the * into a list of all filenames before it actually runs rm. This list
is too long for the command line, thus causing the error. Knowing this,
any method of deleting the files that doesn't require the shell to expand
the list will work as well. There are also tricks you can use to configure
the shell to use a longer command line, but I don't remember details.
Search the list archives. I asked this same question a year or so back, and
the thread that resulted is very informative.
More information about the freebsd-questions