linux中防止误删除

一.脚本执行过程:

1.在/bin/bash中创建4个模拟回收站机制的脚本:remove、undel、ctrash、lrm

2../bashrc中添加alias rm='remove'行.

二.脚本使用方法:

./undel.sh [-u] //直接执行undel.sh脚本将安装4个功能脚本,-u参数用于卸载

remove [files] //用于删除文件和目录,使用rm时自动执行该脚本,且不需要带-rf等参数

undel [-a] [files] //用于恢复文件或目录到当前目录,-a参数将恢复回收站的所有内容到当前目录

lrm //查看回收站的内容

ctrash //清空回收站

#/bin/bash
#-----------Copyright-----------
#bashname: undel.sh
#author  : Sandman
#Date    : 2012-05-29
#-------------------------------
cd /usr/bin
if [ "$1" == "-u" ] ;then
    if [ ! -e remove ];then echo "the script don't run!!!";exit;fi
    cp  ~/.bashrc ~/.bashrc_bak
    cat ~/.bashrc_bak|sed '/alias rm/d' >~/.bashrc       #delete line: alias rm='remove'
    echo "alias rm='rm -i'" >>~/.bashrc                  #add line: alias rm='rm -i'
    source ~/.bashrc
    rm ~/.bashrc_bak
    rm remove undel ctrash lrm                           #delete custom script
    echo "Unstall script successfully!!!"
    exit
fi

for var in remove undel ctrash lrm                      #exist script?
do
    if [ -e $var ]; then
        echo "The script is running!!!"
        exit
    fi
done
touch remove undel ctrash lrm                          #create script
chmod 755 remove undel ctrash lrm
echo '#/bin/bash' >remove                              #write script
echo '#/bin/bash' >undel
echo '#/bin/bash' >ctrash
echo '#/bin/bash' >lrm
echo 'mv -f $@ ~/.trash' >>remove
echo 'if [ $1 == "-a" ];then mv ~/.trash/* ./;else mv ~/.trash/$1 ./;fi' >>undel
echo 'rm -rf ~/trash' >>ctrash
echo 'mkdir ~/.trash'>>ctrash
echo 'ls ~/.trash' >>lrm

cp ~/.bashrc ~/.bashrc_bak
cat ~/.bashrc_bak|sed '/alias rm/d' >~/.bashrc #delete line: alias rm='rm -i'
echo "alias rm='remove'">>~/.bashrc #add line: alias rm='remove'
source ~/.bashrc
echo "script run successfully!!!!!!"

相关推荐