Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A safer rm 1

Status
Not open for further replies.

theotyflos

Programmer
Oct 15, 2000
132
GR
I hope this script will be usefull. It saved my work a lot of times. It works on Sco Openserver 5.0.x, ksh. Put it in your .profile.

Code:
SafeDelete() {
F_OPT=""
I_OPT=""
R_OPT=""
USAGE="usage: rm [-firR] file..."
while getopts firR CHR         # Get options from command line
do
  case $CHR in
  f)   F_OPT=-f ;;
  i)   I_OPT=-i ;;
  r|R) R_OPT=-r ;;
  *)   echo $USAGE
       exit 1 ;;
  esac
done
shift `expr $OPTIND - 1`

if [ $# = 0 ]; then
  echo $USAGE
  exit 1
else
  T=`date +%d_%m_%Y_%T`                    # Extension suffixed to deleted files
  for F in $*
  do
    if [ -e $F ]; then
      D=$(cd `dirname $F`; pwd -P)         # Get file's Directory
      case $D in                           # and check if it is the trashcan
        ~/.trashcan*) REAL_RM=true  ;;
        *)            REAL_RM=false ;;
      esac
      if [ "$REAL_RM" = "true" ]; then     # If in trashcan a real delete...
        \rm $F_OPT $I_OPT $R_OPT $F
      else                                 # ...else a safe delete
        if [ "$D" != "/" ]; then
          mkdir -p ~/.trashcan$D 2>/dev/null
        fi
        BF=`basename $F`
        mv $F_OPT $I_OPT $F ~/.trashcan$D/${BF}_$T
      fi
    else
      echo "rm: $F non-existent"
    fi
  done
fi
}
:
:
Code:
alias rm="SafeDelete"
:
:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top