[Linux-disciples] delete a whole lot of files

Stephen R Laniel steve at laniels.org
Thu Feb 8 14:31:57 EST 2007


On Thu, Feb 08, 2007 at 01:26:14PM -0600, Karl Sokol wrote:
> Followed twenty minutes later by:
>  rm -r -f /home/church/jpgs/
[snip]
> It is a little curious that this worked when rm ~/*.jpg didn't, but all's well
> that ends well

The answer is that when you do

rm ~church/jpgs/*

the shell expands '*' before 'rm' sees it. So the shell
expands that to something like

rm ~church/jpgs/1 ~church/jpgs/2 ~church/jpgs/3 ...

Now, if '*' expands to many thousands of files, you've got a
command line that's many thousands of characters long. Which
is why bash complains.

If instead you do

rm -r ~church/jpgs

bash has no problems, because you're not asking it to expand
'*'. All that 'rm' sees is a single directory. Any
command-line program knows how to treat directories. Indeed,
inside of 'rm', it may well be doing something akin to the
'find' trick that we were telling you about.

The moral is: if you'll be deleting everything from inside a
single directory, then you can just do

rm -r directoryName

whereas if you need to delete just specific files in it, you
should do

rm -r directoryName/*.whatever

And it's probably better to use

find directoryName -iname '*.whatever' -exec rm '{}' \;

in the latter case. In an earlier Linux-Disciples email
which I can't find with a moment of googling, I asked
whether there's any way to automate this -- so that doing an
'rm *' would automatically translate it into the relevant
'find' command. You now see what I was on about.

I hope this has all been educational. As always, feel free
to ask questions.

-- 
Stephen R. Laniel
steve at laniels.org
Cell: +(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://lists.bostoncoop.net/pipermail/linux-disciples/attachments/20070208/cbe1225a/attachment.pgp


More information about the Linux-disciples mailing list