Take the 2-minute tour ×

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?

share|improve this question

2 Answers 2

up vote 9 down vote accepted

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:


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

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.


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)
        echo "Sorry, can only clean from /srv/www and /home"
        exit 1

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).

share|improve this answer
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 Aug 25 '09 at 19:02
interesting. there's no way this script could rm -rf / because the -name has to be 'CVS', right? Just wanna make sure, heh. –  meder Aug 25 '09 at 19:45
@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 Aug 26 '09 at 5:11
awesome. thank you. –  meder Aug 26 '09 at 19:30

A simple way to do would be:

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

share|improve this answer
No, that'll break horribly if there are spaces in the path to any CVS directory. You need find … -print0 | xargs -0 … –  derobert Aug 25 '09 at 18:41
Also, since CVS directories are always uppercase, -iname is just begging for false positives. –  derobert Aug 25 '09 at 18:42

Your Answer


