博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux中的回收站机制,防止rm -rf 事件
阅读量:5113 次
发布时间:2019-06-13

本文共 2924 字,大约阅读时间需要 9 分钟。

下文中的配置文件:config.txt

mkdir -p ~/.trash
alias rm=del
del()
{
mv $@ ~/.trash/
if [ $? -ne 0 ];then
echo -e "\033[31mPlease refer to the error:\033[0m\nmv is rm alias. Please use command:\033[31m rm filename \033[0m or \033[31m rm dirname \033[0m. The deleted item will be moved to\033[31m ~/.trash\033[0m \nTo delete directly, Please use command:\033[31m/bin/rm -rf filename \033[0m or \033[31m /bin/rm -rf dirname \033[0m"
fi
}
cleardel()
{
read -p"clear sure? [Input 'y' or 'Y' to confirm. Input 'n' to cancel]" confirm
[ $confirm == 'y' ] || [ $confirm == 'Y' ] && /bin/rm -rf ~/.trash/*
}

 

########################################################################################

原理:就是将rm 别名到了mv中,只要是rm 删除的都移动到宿主目录中的统一目录下。
#############这个文件是脚本文件,基本上不用修改,除非你的用户家目录没有设置在/home/目录中。
#!/bin/bash
# User:四道风
# Email:1092335851@qq.com
# Date: 201907
echo -e "\033[31m请根据提示来设置是否需要将用户配置回收站\033[0m"
echo -e "\033[31m请按提示输入,请勿输入其他字符,否则脚本将自动退出\033[0m"
echo -e "\033[31m配置完成后,如使回收站生效,请断开当前连接,重新连接(重新加载环境变量)\033[0m"
#列出系统可登陆用户
users=`grep /bin/bash /etc/passwd | awk -F ":" '{print $1}' > ./user.txt`
for U in `cat ./user.txt`
do
read -p "user is $U ,please input yes/YES or no/NO : " input
if [[ $input == 'yes' || $input == 'YES' ]];then
if [[ $U == 'root' ]];then
grep "^del()" /root/.bashrc > /dev/null 2>&1
if [ $? -eq 0 ];then
echo -e "\033[31m/root/.bashrc\033[0m .The del function already exists.Trach already exists.Automatic jump out"
continue
fi
if [ $? -eq 0 ];then
sed -i "s/alias rm='rm -i'/#alias rm='rm -i'/" /root/.bashrc > /dev/null 2>&1 && cat ./config.txt >> /root/.bashrc && echo "A recycle bin has been set up,The relogin will take effect! ! ! " && source /root/.bashrc
else
echo "/root/.bashrc is not:(alisa rm='rm -i')"
egrep -v '^$|^#' /root/.bashrc > /root/.bashrc > /dev/null 2>&1
cat ./config.txt >> /root/.bashrc && echo "A recycle bin has been set up,The relogin will take effect! ! ! " && source /root/.bashrc
fi
else
grep "^del()" /home/$U/.bashrc > /dev/null 2>&1
if [ $? -eq 0 ];then
echo -e "\033[31m/home/$U/.bashrc\033[0m .The del function already existsi.Trach already exists .Automatic jump out"
continue
fi
#mkdir -p /home/$U/.trash
if [ $? -eq 0 ];then
sed -i "s/alias rm='rm -i'/#alias rm='rm -i'/" /home/$U/.bashrc > /dev/null 2>&1 && cat ./config.txt >> /home/$U/.bashrc && echo "A recycle bin has been set up,The relogin will take effect! ! ! " && source /home/$U/.bashrc
else
echo "$U is not (alias rm='rm -i')"
egrep -v '^$|^#' /home/$U/.bashrc > /home/$U/.bashrc > /dev/null 2>&1
cat ./config.txt >> /home/$U/.bashrc && echo "A recycle bin has been set up,The relogin will take effect ! ! ! " && source /home/$U/.bashrc
fi
fi
elif
[[ $input == 'no' || $input == 'NO' ]];then
continue
else
echo -e "Please enter \033[31m[ no | NO | yes | YES ]\033[0m \nplease do not enter other, otherwise it will exit the script directly."
break
fi

done

 

转载于:https://www.cnblogs.com/sidaofeng/p/11130992.html

你可能感兴趣的文章
yii 跳转页面
查看>>
洛谷 1449——后缀表达式(线性数据结构)
查看>>
Data truncation: Out of range value for column 'Quality' at row 1
查看>>
Dirichlet分布深入理解
查看>>
(转)Android之发送短信的两种方式
查看>>
字符串处理
查看>>
HtmlUnitDriver 网页内容动态抓取
查看>>
ad logon hour
查看>>
获得进程可执行文件的路径: GetModuleFileNameEx, GetProcessImageFileName, QueryFullProcessImageName...
查看>>
证件照(1寸2寸)拍摄处理知识汇总
查看>>
罗马数字与阿拉伯数字转换
查看>>
Eclipse 反编译之 JadClipse
查看>>
Python入门-函数
查看>>
[HDU5727]Necklace(二分图最大匹配,枚举)
查看>>
距离公式汇总以及Python实现
查看>>
设计模式之装饰者模式
查看>>
一道不知道哪里来的容斥题
查看>>
Blender Python UV 学习
查看>>
window添加右键菜单
查看>>
入手腾龙SP AF90mm MACRO
查看>>