Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Script to recursively delete CVS directory on server

Tags:

bash

unix

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?

like image 481
meder omuraliev Avatar asked Aug 25 '09 18:08

meder omuraliev


2 Answers

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.
# or find … -exec rm -Rf '{}' +   if you have reasonably modern find

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

like image 53
derobert Avatar answered Nov 16 '22 00:11

derobert


A simple way to do would be:

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

like image 34
choudeshell Avatar answered Nov 15 '22 22:11

choudeshell