Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

"rm -rf *" - "argument list too long"

1,164 views
Skip to first unread message

Darren Wyn Rees

unread,
May 8, 2000, 3:00:00 AM5/8/00
to
I'm in a directory with a tonne of files I wish to delete.
I type "rm -rf *", but it won't work, it gives "/bin/rm: Argument
list too long". So how can I delete these many files ?

I can "cd ..", and then "rm -rf <directory name>", and that works.

However, what if I didn't wish to remove directories. In that
case, what would I do to remove the files ?

Thanks

darren

PS. (I'm finding it takes some getting used to putting
the operands to many commands immediately after the command.
Why is this so with FreeBSD? With Linux I can type 'rm * -rf' etc.)

To Unsubscribe: send mail to majo...@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message

Harold Pulcher - Killer Computing

unread,
May 8, 2000, 3:00:00 AM5/8/00
to
do a man xargs. I think that will do what you need.

Harold

Olivier Cortes

unread,
May 8, 2000, 3:00:00 AM5/8/00
to
for FILE in `ls -1 DIR/`
do
rm "$FILE"
done

shell script. it doesn't work if files have spaces in their names.

have fun :)

Olivier

> -----Message d'origine-----
> De : owner-freeb...@FreeBSD.ORG
> [mailto:owner-freeb...@FreeBSD.ORG]De la part de Darren Wyn
> Rees
> Envoyé : dimanche 7 mai 2000 22:14
> À : freebsd-...@freebsd.org
> Objet : "rm -rf *" - "argument list too long"

Crist J. Clark

unread,
May 8, 2000, 3:00:00 AM5/8/00
to
On Sun, May 07, 2000 at 08:14:10PM +0000, Darren Wyn Rees wrote:
> I'm in a directory with a tonne of files I wish to delete.
> I type "rm -rf *", but it won't work, it gives "/bin/rm: Argument
> list too long". So how can I delete these many files ?
>
> I can "cd ..", and then "rm -rf <directory name>", and that works.
>
> However, what if I didn't wish to remove directories. In that
> case, what would I do to remove the files ?

Someone already pointed to xargs(1).

> PS. (I'm finding it takes some getting used to putting
> the operands to many commands immediately after the command.
> Why is this so with FreeBSD? With Linux I can type 'rm * -rf' etc.)

% man rm
.
.
.
NOTE
The rm command uses getopt(3) to parse its arguments, which allows it to
accept the `--' option which will cause it to stop processing flag op-
tions at that point. This will allow the removal of file names that be-
gin with a dash (`-'). For example:
rm -- -filename
The same behavior can be obtained by using an absolute or relative path
reference. For example:
rm /home/user/-filename
rm ./-filename

See the getopt(3) manpage to see more detail why,

% rm * -rf

Would not work. With the syntax you describe the above command chokes
if there is a file named, say, '-filename' in the pwd.
--
Crist J. Clark cjc...@home.com

Ben Smithurst

unread,
May 8, 2000, 3:00:00 AM5/8/00
to
Darren Wyn Rees wrote:

> I'm in a directory with a tonne of files I wish to delete.
> I type "rm -rf *", but it won't work, it gives "/bin/rm: Argument
> list too long". So how can I delete these many files ?

find . -delete

> I can "cd ..", and then "rm -rf <directory name>", and that works.
>
> However, what if I didn't wish to remove directories. In that
> case, what would I do to remove the files ?

find . -type f -delete

Replace "-delete" with "-print | xargs rm -f" for a more portable
version, with only a small amount of extra overhead.

> PS. (I'm finding it takes some getting used to putting
> the operands to many commands immediately after the command.
> Why is this so with FreeBSD? With Linux I can type 'rm * -rf' etc.)

uh.... If you can really do that with Linux, that's severely weird.
It's just common sense as far as I can see to have the options first, I
guess it's because that's what I'm used to.

--
Ben Smithurst / b...@scientia.demon.co.uk / PGP: 0x99392F7D

Ben Smithurst

unread,
May 8, 2000, 3:00:00 AM5/8/00
to
Ben Smithurst wrote:

> Darren Wyn Rees wrote:
>
>> I'm in a directory with a tonne of files I wish to delete.
>> I type "rm -rf *", but it won't work, it gives "/bin/rm: Argument
>> list too long". So how can I delete these many files ?
>
> find . -delete

Sorry, I forgot to mention that these aren't quite the same... My
version will delete files whose names begin with a dot, yours won't as
the glob expansion won't include those files. Fixing that is left as an
exercise.

Jim Conner

unread,
May 8, 2000, 3:00:00 AM5/8/00
to
At 09:46 PM 5/7/00 +0100, Ben Smithurst wrote:
>Darren Wyn Rees wrote:
>
> > I'm in a directory with a tonne of files I wish to delete.
> > I type "rm -rf *", but it won't work, it gives "/bin/rm: Argument
> > list too long". So how can I delete these many files ?
>
>find . -delete
>
> > I can "cd ..", and then "rm -rf <directory name>", and that works.
> >
> > However, what if I didn't wish to remove directories. In that
> > case, what would I do to remove the files ?
>
>find . -type f -delete
>
>Replace "-delete" with "-print | xargs rm -f" for a more portable
>version, with only a small amount of extra overhead.
>
> > PS. (I'm finding it takes some getting used to putting
> > the operands to many commands immediately after the command.
> > Why is this so with FreeBSD? With Linux I can type 'rm * -rf' etc.)
>
>uh.... If you can really do that with Linux, that's severely weird.
>It's just common sense as far as I can see to have the options first, I
>guess it's because that's what I'm used to.

Wierd? Hmm...perhaps...but it is probably something Linux does. I have
personally never tried it but I have noticed that Linux bins (some not all)
have the capability to accept "post-"arguments. For instance...the ls
command accepts arguments like:

ls filename -al

However, BSD won't accept this. Apparently it is something against the
POSIX standard??? I dunno about that. But the idea that Linux's rm
command accepts post-arguments doesn't surprise me. I don't really have an
opinion for or against it either. Personally, it makes sense to me to put
the arguments before the list however, apparently there are people out
there who prefer the opposing.

- Jim


>--
>Ben Smithurst / b...@scientia.demon.co.uk / PGP: 0x99392F7D
>
>
>To Unsubscribe: send mail to majo...@FreeBSD.org
>with "unsubscribe freebsd-questions" in the body of the message


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Today's errors, in contrast:
Windows - "Invalid page fault in module kernel32.dll at 0032:A16F2935"
UNIX - "segmentation fault - core dumped"
Humanous Beingsus - "OOPS, I've fallen and I can't get up"
-------------------------------
Jim Conner
NOTJames
jco...@enterit.com

Doug Barton

unread,
May 8, 2000, 3:00:00 AM5/8/00
to
Darren Wyn Rees wrote:
>
> I'm in a directory with a tonne of files I wish to delete.
> I type "rm -rf *", but it won't work, it gives "/bin/rm: Argument
> list too long". So how can I delete these many files ?
>
> I can "cd ..", and then "rm -rf <directory name>", and that works.
>
> However, what if I didn't wish to remove directories. In that
> case, what would I do to remove the files ?

First, ignore the overly complicated and potentially dangerous answers
so far on this thread. :) Second, think about it for a minute. If "*"
provides a list that's too long for rm to handle, why not break it up
into smaller chunks? For example, if your files mainly have alpha
characters in their names, do something like:

rm [a-c]*

Starting with larger chunks, and going smaller as needed till the error
messages disappear.

Doug
--
"Live free or die"
- State motto of my ancestral homeland, New Hampshire

Do YOU Yahoo!?

Olivier Cortes

unread,
May 8, 2000, 3:00:00 AM5/8/00
to
yeah !
great answer !

I use to be too sophisticated.
thanks to pull me back to simplicity :)

Olivier

PS: but don't you think a little program written in perl ou c could.... Okay, i shut up :)

> -----Message d'origine-----
> De : owner-freeb...@FreeBSD.ORG

> [mailto:owner-freeb...@FreeBSD.ORG]De la part de Doug Barton
> Envoyé : lundi 8 mai 2000 01:51
> À : Darren Wyn Rees
> Cc : freebsd-...@freebsd.org
> Objet : Re: "rm -rf *" - "argument list too long"

Steve Price

unread,
May 8, 2000, 3:00:00 AM5/8/00
to
On Sun, 7 May 2000, Doug Barton wrote:

# First, ignore the overly complicated and potentially dangerous answers
# so far on this thread. :) Second, think about it for a minute. If "*"
# provides a list that's too long for rm to handle, why not break it up
# into smaller chunks? For example, if your files mainly have alpha
# characters in their names, do something like:
#
# rm [a-c]*
#
# Starting with larger chunks, and going smaller as needed till the error
# messages disappear.

Something like this doesn't work?

ls * | xargs rm

Seems simple enough and should do it all in one pass. :)

-steve

Darren Wyn Rees

unread,
May 8, 2000, 3:00:00 AM5/8/00
to
On Sun, May 07, 2000 at 04:50:57PM -0700, Doug Barton wrote:

> First, ignore the overly complicated and potentially dangerous answers

> so far on this thread. :) Second, think about it for a minute. If "*"

> provides a list that's too long for rm to handle, why not break it up

> into smaller chunks?

Good suggestion... And it's what I did (though I didn't mention it).
However, what I found was that it was still taking lots of time
to "break it up"... eg. I'd try rm *54* and it wouldn't work,
so I'd try rm *53* and it wouldn't work... but then, rm *52* would.
I didn't try using a [range], so your suggestion was helpful. Thanks.

Ben Smithurst

unread,
May 8, 2000, 3:00:00 AM5/8/00
to
Doug Barton wrote:

> First, ignore the overly complicated and potentially dangerous answers
> so far on this thread. :)

Well, if you bothered to say which ones people might have a chance of
knowing which to avoid.

--
Ben Smithurst / b...@scientia.demon.co.uk / PGP: 0x99392F7D

Doug Barton

unread,
May 8, 2000, 3:00:00 AM5/8/00
to
Steve Price wrote:
>
> On Sun, 7 May 2000, Doug Barton wrote:
>
> # First, ignore the overly complicated and potentially dangerous answers
> # so far on this thread. :) Second, think about it for a minute. If "*"
> # provides a list that's too long for rm to handle, why not break it up
> # into smaller chunks? For example, if your files mainly have alpha
> # characters in their names, do something like:
> #
> # rm [a-c]*
> #
> # Starting with larger chunks, and going smaller as needed till the error
> # messages disappear.
>
> Something like this doesn't work?
>
> ls * | xargs rm
>
> Seems simple enough and should do it all in one pass. :)

Most of the proposed solutions would work, but the ones like this
suffer from having to spawn a new process for each file. Depending on
how many files are in the directory, this could take a very long time
(where "long time" is relative of course). A little shell manipulation
goes a long way here.

Doug
--
"Live free or die"
- State motto of my ancestral homeland, New Hampshire

Do YOU Yahoo!?


Eric J. Schwertfeger

unread,
May 8, 2000, 3:00:00 AM5/8/00
to
On Sun, 7 May 2000, Doug Barton wrote:

> Steve Price wrote:
> >
> > Something like this doesn't work?
> >
> > ls * | xargs rm
> >
> > Seems simple enough and should do it all in one pass. :)
>
> Most of the proposed solutions would work, but the ones like this
> suffer from having to spawn a new process for each file. Depending on
> how many files are in the directory, this could take a very long time
> (where "long time" is relative of course). A little shell manipulation
> goes a long way here.

Actually, this one won't create one process per file. xargs by default
packs multiple arguments to each call of the executed program, .
FreeBSD's xargs defaults to 5000 args or 63488 characters, whichever comes
first, which is to say it's coming reasonably close to the best case, so
you're not going to beat it by much unless you avoid shell globbing all
together.

My normal solution to this (yes, I hit it a lot at work) is either xargs
or move the directory to a different name, create a new directory with the
original name, mv the directories in the old directory into the new one,
then rm -rf the old directory. Depends on the mood and the OS, we've got
an old crufty SysV unix at work with a flakey xargs command.

William Melanson

unread,
May 13, 2000, 3:00:00 AM5/13/00
to
On Sun, 7 May 2000, Steve Price wrote:

% On Sun, 7 May 2000, Doug Barton wrote:
%
% # First, ignore the overly complicated and potentially dangerous answers
% # so far on this thread. :) Second, think about it for a minute. If "*"
% # provides a list that's too long for rm to handle, why not break it up
% # into smaller chunks? For example, if your files mainly have alpha
% # characters in their names, do something like:
% #
% # rm [a-c]*
% #
% # Starting with larger chunks, and going smaller as needed till the error
% # messages disappear.
%
% Something like this doesn't work?
%
% ls * | xargs rm
%
% Seems simple enough and should do it all in one pass. :)
%
% -steve
%
%
%
% To Unsubscribe: send mail to majo...@FreeBSD.org
% with "unsubscribe freebsd-questions" in the body of the message
%

I know this sounds a bit crazy but how about this:

rm -i ./* (please be in the directory of choice)

Then and "yes" and "no" them as you cruise along? It may take a bit of
time but at least you don't kiss the good guys bye, bye...

An once of prevention... :)

--------------------------------oOo------------------------------------
William J. Melanson CyberGate, Inc. | e.spire Communications
Sr Network Controller www.gate.net ---- www.espire.net
Network Operations Center Phone: (954) 334-8080
finger w...@gate.net PGP public key
--------------------------------oOo------------------------------------

Trevor Johnson

unread,
May 13, 2000, 3:00:00 AM5/13/00
to
> I'm in a directory with a tonne of files I wish to delete.
> I type "rm -rf *", but it won't work, it gives "/bin/rm: Argument
> list too long". So how can I delete these many files ?
>
> I can "cd ..", and then "rm -rf <directory name>", and that works.
>
> However, what if I didn't wish to remove directories. In that
> case, what would I do to remove the files ?

This will recursively remove just the plain files, leaving behind
directories, symlinks, pipes, and device files:

find <directory name> -type f -exec rm \{\} \;

With your shell, you might not have to use as many escapes.
--
Trevor Johnson
http://jpj.net/~trevor/gpgkey.txt

0 new messages