I'm always nervous of bash scripts that do `cd $dir ; rm -rf *` (or equivalent) because if the `cd $d` fails, the `rm -rf *` is still executed.  I'd strongly suggest changing this to either be a `find $dir ...` or (less preferably) `cd $d && find . ...`.