在Linux对rm进行优化,避免误删文件

来源:互联网 发布:最新网络诈骗阿里巴巴 编辑:程序博客网 时间:2024/05/29 13:55
#!/bin/sh# newrm, a replacement for the existing rm command, 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, and 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!mydir="$HOME/.deleted-files"realrm="/bin/rm"copy="/bin/cp -R"if [ $# -eq 0 ] ; then # let 'rm' ouptut the usage error  exec $realrm # our shell is replaced by /bin/rmfi# Parse all options looking for '-f'flags=""while getopts "dfiPRrvW" optdo  case $opt in    f) exec $realrm "$@"      ;;  # exec lets us exit this script directly.    *) flags="$flags -$opt"   ;;  # other flags are for 'rm', not us  esacdoneshift $(($OPTIND - 1))# Make sure that the $mydir existsif [ ! -d $mydir ] ; then  if [ ! -w $HOME ] ; then    echo "$0 failed: can't create $mydir in $HOME" >&2    exit 1  fi  mkdir $mydir  chmod 700 $mydir      # a little bit of privacy, pleasefifor argdo  newname="$mydir/$(date "+%S.%M.%H.%d.%m").$(basename "$arg")"  if [ -f "$arg" ] ; then    $copy "$arg" "$newname"  elif [ -d "$arg" ] ; then    $copy "$arg" "$newname"  fidoneexec $realrm $flags "$@"        # our shell is replaced by realrm
alias rm=yourpath/newrm
原创粉丝点击