linux中的回收站机制,防止rm -rf 事件

下文中的配置文件:config.txt
mkdir -p ~/.trash
alias rm=del
del()
{
mv [email protected] ~/.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:[email protected]
# 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

时间: 2024-07-31 02:49:06

linux中的回收站机制,防止rm -rf 事件的相关文章

linux中自定义回收站

myrm(){ D=/tmp/$(date +%Y%m%d%H%M%S); mkdir -p $D; mv "[email protected]" $D && echo "moved to $D ok"; } [[email protected] test]# myrm(){ D=/tmp/$(date +%Y%m%d%H%M%S); mkdir -p $D;  mv "[email protected]" $D &&am

浅谈Linux中的信号机制(二)

首先谢谢 @小尧弟 这位朋友对我昨天夜里写的一篇<浅谈Linux中的信号机制(一)>的指正,之前的题目我用的“浅析”一词,给人一种要剖析内核的感觉.本人自知功力不够,尚且不能对着Linux内核源码评头论足.以后的路还很长,我还是一步一个脚印的慢慢走着吧,Linux内核这座山,我才刚刚抵达山脚下. 好了,言归正传,我接着昨天写下去.如有错误还请各位看官指正,先此谢过. 上篇末尾,我们看到了这样的现象:send进程总共发送了500次SIGINT信号给rcv进程,但是实际过程中rcv只接受/处理了1

Linux中的保护机制

Linux中的保护机制 在编写漏洞利用代码的时候,需要特别注意目标进程是否开启了NX.PIE等机制,例如存在NX的话就不能直接执行栈上的数据,存在PIE 的话各个系统调用的地址就是随机化的. 一:canary(栈保护) 栈溢出保护是一种缓冲区溢出攻击缓解手段,当函数存在缓冲区溢出攻击漏洞时,攻击者可以覆盖栈上的返回地址来让shellcode能够得到执行.当启用栈保护后,函数开始执行的时候会先往栈里插入cookie信息,当函数真正返回的时候会验证cookie信息是否合法,如果不合法就停止程序运行.

linux中一些基本命令tar,cp,rm,touch,mkdir等

tar 解压缩命令: tar -c 建立压缩档案 tar -x 解压档案 tar -t 查看内容 以下是文件属性解压方式 -z解压gzip属性  -j解压bz2属性 -v显示过程 其中-f是必须的,-f是解压的最后一个参数,后面接档案名: 例如: 解压: tar -xvf file.tar  ---解压tar文件 tar -xzvf file.tar.gz ---解压tar.gz文件 tar -xjvf   file.tar.bz2 ---解压bz2文件 压缩: tar -cvf file.ta

linux中的signal机制(转)

信号是Linux编程中非常重要的部分,本文将详细介绍信号机制的基本概念.Linux对信号机制的大致实现方法.如何使用信号,以及有关信号的几个系统调用. 信号机制是进程之间相互传递消息的一种方法,信号全称为软中断信号,也有人称作软中断.从它的命名可以看出,它的实质和使用很象中断.所以,信号可以说是进程控制的一部分. 一.信号的基本概念 本节先介绍信号的一些基本概念,然后给出一些基本的信号类型和信号对应的事件.基本概念对于理解和使用信号,对于理解信号机制都特别重要.下面就来看看什么是信号. 1.基本

浅析Linux中的信号机制(一)

有好些日子没有写博客了,自己想想还是不要荒废了时间,写点儿东西记录自己的成长还是百利无一害的.今天是9月17号,暑假在某家游戏公司实习了一段时间,做的事情是在Windows上用c++写一些游戏英雄技能的逻辑实现.虽然时间不算长,但是也算学了一点东西,对团队项目开发流程也有了一个直观的感受,项目里c++11新特性也有用到不少,特别是lambda表达式,STL的一些容器和算法也终于有了可以实践的地方.由于自己比较喜欢Linux C,也就没有做留下的打算,现在回到了学校,好好复习一段时间,准备一下校招

Android中的常见通信机制和Linux中的通信机制

Handler Handler是Android系统中的一种消息传递机制,起作用是应对多线程场景.将A进程的消息传递给B线程,实现异步消息处理.很多情况是将工作线程中需要更新UI的操作消息传递给UI主线程,而实现更新UI操作. 因为工作线程和主线程是共享地址空间,即Handler实例对象mHandler位于线程间共享的内存堆上,工作线程和主线程直接使用该对象,只需要注意多线程的同步问题.工作系统通过mHandler向其成员变量MessageQueue中添加Message,而主线程一直处于loop中

使用垃圾桶机制防止rm -rf误删文件

偶然看到一个比较好用的工具Trash-Cli.一个类似垃圾桶的机制,可以恢复文件.试了一下,感觉还行 (1)下载安装: https://github.com/andreafrancia/trash-cli [[email protected] ~]# yum install git -y #安装git [[email protected] ~]#git clone https://github.com/andreafrancia/trash-cli.git #git克隆岛本地 [[email p

linux系统下文件夹以及文件的权限查看及修改;以及文件和文件夹删除 rm -rf 命令的使用

1.直接查看文件或者文件夹的权限命令: >>ll 或者 >>ls -l r代表读取权限,w写权限,x代表执行权限 第一个代表文件类型,-代表是普通文件,d代表是文件夹 权限一共分成3组,3个一组,分别是所有者,所属组,其他人 2.修改文件权限: 权限管理命令chmod,全拼:chang permissions mode of a file 语法:chmod [{ugoa}{+-=}{rwx}][文件或目录] [mode=421][文件或目录] -R 递归修改 u:所有者,g:所属组