#!/bin/bash # newrm--A replacement for the existing rm command. # This script provides a # rudimentary unremove capability by creating and utilizing a new # directory within the user's home directory. It can handle directories # of content as well as individual files. If the user specifies # the -f flag, files are removed and NOT archived. # Big Important Warning: You'll want a cron job or something similar to keep # the trash directories tamed. Otherwise, nothing will ever actually # be deleted from the system, and you'll run out of disk space! setglobal archivedir = ""$HOME/.deleted-files"" setglobal realrm = $[which rm] setglobal copy = ""$[which cp] -R"" if test $Argc -eq 0 { # let 'rm' output the usage error exec $realrm # our shell is replaced by /bin/rm } # Parse all options looking for '-f' setglobal flags = ''"" while getopts "dfiPRrvW" opt { match $opt { with f exec $realrm @Argv # exec lets us exit this script directly. with * setglobal flags = ""$flags -$opt"" # other flags are for 'rm', not us } } shift $shExpr(' $OPTIND - 1 ') # BEGIN MAIN SCRIPT # ================= # Make sure that the $archivedir exists if test ! -d $archivedir { if test ! -w $HOME { echo "$0 failed: can't create $archivedir in $HOME" > !2 exit 1 } mkdir $archivedir chmod 700 $archivedir# a little bit of privacy, please }for arg in @Argv { setglobal newname = ""$archivedir/$[date "+%S.%M.%H.%d.%m].$[basename $arg]"" if test -f $arg -o -d $arg { $copy $arg $newname } } exec $realrm $flags @Argv # our shell is replaced by realrm