「shell」替代rm,放入回收站

今天测试个东西,不小心使用了rm -rf命令,导致整个项目被删除,幸好刚已上传到git上。

禁止使用rm命令,用rmtrash替代rm命令,文件不直接删除,而是放入到/tmp目录下

 

该脚本文件名为rmtrash,并放入/usr/local/bin目录下。

#!/bin/sh
 
# script to send removed files to trash directory
t=`date "+%G-%m-%d_%H.%M.%S"`
trash=/tmp/.trash
mkdir -p $trash

for i in "$@"; do
    if [ "X$i" == "X" ];then
        continue
    fi

    filename=`basename $i`
    path="$(cd `dirname $i`; pwd)"/$filename
    newName=${filename}_${t}
    rm -rf ${trash}/${newName}
    mv -f ${path} ${trash}/${newName}
done

 

posted on 2018-05-30 17:11  trako  阅读(221)  评论(0编辑  收藏  举报

导航