A safe program to replace unix command ‘rm’

#!/usr/bin/perl -w

## A safer program to replace unix command "rm"
## Author: Hongyu Zhang

if($#ARGV < 0) {
    die("Usage:  $0 filename\n");
}

$dir = `echo ~/dumpster`;
chomp $dir;
mkdir $dir unless(-d $dir);

for($i=0; $i<=$#ARGV; $i++) {
    croak("Directory $ARGV[$i] not existed") unless(-e $ARGV[$i]);
    my @arr = split /\//, $ARGV[$i];
    if(-e "$dir/$arr[$#arr]") {
        $command = "rm -rf $dir/$arr[$#arr]";
        system("$command");
        croak("error running command: $command") if($?);
    }

    $command = "mv -fv $ARGV[$i] $dir";
    system("$command");
    croak("error running command: $command") if($?);
}

Leave a Reply