#!/usr/bin/perl # # Copyright (C) 2002 by Ketil Froyn # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or (at # your option) any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 # USA # # # Quick'n'dirty script to remove directories recursively # use strict; my $i; my $cur=`pwd`; chomp $cur; for ($i = 0; $i <= $#ARGV; $i++) { if (-d $ARGV[$i]) { if (!rmdir($ARGV[$i])) { chdir($ARGV[$i]); rm_rf(); chdir($cur); rmdir($ARGV[$i]); } } else { unlink($ARGV[$i]); } } sub rm_rf { my $d = 0; my $f; while (1) { opendir D, "."; my $e; $f = ".."; # This is the default dir to move into while (($e = readdir(D))) { next if ($e eq "."); next if ($e eq ".."); if (-d $e) { unless (rmdir($e)) { # Try to rmdir directories when found $f = $e; # If that didn't work, we'll recurse down last; } print "Remove: $e (dir)\n"; } else { print "Remove: $e (file)\n"; unlink($e); } } closedir D; if ($f eq "..") { $d--; } else { $d++; } last if ($d < 0); chdir $f; } }