防止rm误操作

为了防止误操作导致删除重要文件,特写以下代码

alias rm="/bin/sh /bin/denyrm.sh"

vim /bin/denyrm.sh


#/!bin/bash
#防止rm重要目录
#当同时使用-f和-r时,才生效
#可使用killit强制删除,否则移至/tmp
#writted by xly

if [ $# -eq 0 ];then
  echo >&2 'rm:' $"missing file arguments"
  echo >&2 $"Try \`rm --help' for more information."
  exit 1
fi
force=
recursive=
killit=
while test $# -gt 0; do
  case "$1" in
  --h | --he | --hel | --help)
        echo $"Usage: rm [OPTION]... FILE...
        Remove (unlink) the FILE(s).

        -f, --force           ignore nonexistent files, never prompt
        -i, --interactive     prompt before any removal
                --no-preserve-root do not treat \`/' specially (the default)
                --preserve-root   fail to operate recursively on \`/'
        -r, -R, --recursive   remove directories and their contents recursively
        -v, --verbose         explain what is being done
                --killit        igonre the limit, remove it now
                --help     display this help and exit
                --version  output version information and exit

        By default, rm does not remove directories.  Use the --recursive (-r or -R)
        option to remove each listed directory, too, along with all of its contents.

        To remove a file whose name starts with a \`-', for example \`-foo',
        use one of these commands:
        rm -- -foo

        rm ./-foo

        Note that if you use rm to remove a file, it is usually possible to recover
        the contents of that file.  If you want more assurance that the contents are
        truly unrecoverable, consider using shred.

        Report bugs to <bug-coreutils@gnu.org>."
        exit 0
    ;;
  *)
    break
    ;;
  esac
done

#can not rm
function rm_no {
 dir=$1
 must=( "/" "/bin" "/boot" "/data" "/dev" "/dist" "/etc" "/home" "/lib" "/lost+found" "/media" "/misc" "/mnt" "/net" "/opt" "/proc" "/root" "/sbin" "/selinux" "/srv" "/sys" "/tmp" "/usr" "/var" "/usr/local/db1" "/usr/local/db2")
#the dir or it's pdir in the array ,can not rm
  if [ -z "$dir" ];then
   dir_tmp1=`pwd`
  elif [ -d "$dir" ];then
   dir_tmp1=`cd $dir&&pwd`
  fi

  dir_tmp2=`cd ${dir_tmp1}/../&&pwd` 
  for((i=0;i<=${#must[*]};i++))
  do
   if [ "$dir_tmp1" == "${must[i]}" -o "$dir_tmp2" == "${must[i]}" ];then
    echo " can't rm $dir "
    exit 1;
   fi
  done 
  
  #mv
  tmp3=`echo $dir_tmp1|awk -F'/' '{print $NF}'|tr -d '/'`
  if [ -f /tmp/$tmp3 -o -d /tmp/$tmp3 ];then
   mv /tmp/$tmp3 /tmp/${tmp3}_`date +%F%T`
   mv $dir /tmp/
  else
   mv $dir /tmp/
  fi

}

#rm anyway
function rm_yes {
 rm -fr $1
 exit 0
}

#-fr
tmp=`echo $* |awk -F' ' '{for(i=1;i<=NF;i++){if($i~/^-[a-z]/){print $i}}}'`
for i in $tmp
do
 tmp1=`echo $i|sed -r 's/-[f,i,r,v,R]*//'`
 if [ -z $tmp1 -a `echo $i|grep -e 'r\|R'` ];then
   recursive=yes
 fi
 if [ -z $tmp1 -a `echo $i|grep 'f'` ];then
   force=yes
 fi
done

#--killit
tmp=`echo $* |grep ' \-\-killit'`
if [ -z "$tmp" ];then
 killit=
else
 killit=yes;
fi

#main
for file in `echo "$*"`
do
 if [ -z `echo $file|sed -r 's/-[f,i,r,v,P]*//'` ];then
  continue;
 fi
 if [ "$force" == "yes" -a "$recursive" == "yes" -a -z "$killit" ];then
  rm_no $file
 elif [ "$force" == "yes" -a "$recursive" == "yes" -a "$killit" == "yes" ];then
  rm_yes $file
 else
  rm -i $*
 fi
done


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

向良玉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值