#!/usr/bin/perl $version="prunedir, version 1.10 (c) torh\@bogus.net 14-mar-2008"; # prunedir - moves or removes excess directories from a directory # oal; hope this meets your requirements! :-) # # 1.00 - first release # 1.10 - added functionality; stuff the directories you want to ignore # into the following array @IGNORE= ("projects","ruby","Library","perl","backup","homepage"); ## THE CHECK IS CASE SENSITIVE # handle with care! # $|=1; # use Getopt::Long; GetOptions("h","i","r","p", "d=s" => \$target_dir, "s=s" => \$dir, "n=s" => \$toomany); if($opt_h) { print <] [-s ] [-n ] [-r] [-i] [-p] -h : this -p : don't pretend (actually do the prune) -r : remove excess directories instead of moving them -d : specify destination directory (overridden if -r specified) -s : directory to prune -n : directories to keep (newest directories deleted last) -i : print information during execution EOM exit(0); } $target_dir="/tmp" unless($target_dir); $dir="." unless($dir); $toomany=10 unless($toomany);; if($opt_i) { $date = `date '+%H:%M:%S %d-%h-%Y'`; chomp($date); print <) { my($ent) = $_; chomp($ent); if(-d "$dir/$ent") { if($toomany>0 || Keep($ent)) { print "KEEP: $ent\n"; } else { $cmd="mv '$dir/$ent' '$target_dir'" unless($opt_r); $cmd="rm -rf '$dir/$ent'" if($opt_r); $res=`$cmd` if($opt_p); $res="Would execute: $cmd (rerun with -p?)" unless($opt_p); if($res) { print "Error? $res" if($opt_p); print $res,"\n" unless($opt_p); } else { print "MOVED: $ent\n" unless($opt_r); print "DELETED: $ent\n" if($opt_r); } } $toomany-- unless(Keep($ent)); # decrement only if it's not on ignore list } } close(DIR); exit; sub Keep { my($dir) = @_; foreach(@IGNORE) { return(1) if($_ eq $dir); } return(0); }