tags:

views:

471

answers:

2

So far I've come up with this:

find . -name 'CVS' -type d -exec rm -rf {} \;

It's worked locally thus far, can anyone see any potential issues? I want this to basically recursively delete 'CVS' directories accidently uploaded on to a server.

Also, how can I make it a script in which I can specify a directory to clean up?

A: 

A simple way to do would be:

find . -iname CVS -type d | xargs rm -rf

choudeshell
No, that'll break horribly if there are spaces in the path to any CVS directory. You need `find … -print0 | xargs -0 …`
derobert
Also, since CVS directories are always uppercase, `-iname` is just begging for false positives.
derobert
+4  A: 

Well, the obvious caveat: It'll delete directories named CVS, regardless of if they're CVS directories or not.

You can turn it into a script fairly easily:

#!/bin/sh

if [ -z "$1" ]; then
    echo "Usage: $0 path"
    exit 1
fi

find "$1" -name 'CVS' -type d -print0 | xargs -0 rm -Rf
# or find … -exec like you have, if you can't use -print0/xargs -0
# print0/xargs will be slightly faster.

edit

If you want to make it safer/more fool-proof, you could do something like this after the first if/fi block (there are several ways to write this):

⋮
case "$1" in
    /srv/www* | /home)
        true
        ;;
    *)
        echo "Sorry, can only clean from /srv/www and /home"
        exit 1
        ;;
esac
⋮

You can make it as fancy as you want (for example, instead of aborting, it could prompt if you really meant to do that). Or you could make it resolve relative paths, so you wouldn't have to always specify a full path (but then again, maybe you want that, to be safer).

derobert
The main concern with using xargs like this is that with enough matching directories, you might overflow the size of the command line passed to "rm -Rf". The limit provided by the default value for "-s" is supposed to prevent xargs from doing that, I still run into it on some platforms; that check doesn't seem to be very reliable everywhere. To avoid any problem here, I always use "-n 1" as one argument to xargs, so I get the benefit processing all piped input, with no overflow concerns. At that point you might as well use exec because the efficient gain you were aiming for goes away.
Greg Smith
interesting. there's no way this script could rm -rf / because the -name has to be 'CVS', right? Just wanna make sure, heh.
meder
@meder: Correct, it can't `rm -Rf /` because of `-name`. It can, of course, happily remove the CVS repository sitting in `/srv/CVS`. You may wish to add in further tests to make it safer.
derobert
awesome. thank you.
meder