文章参考:https://www.linuxprobe.com/rm-saferm-trash.html
文章参考:https://www.cnblogs.com/kerrycode/p/12931844.html

  1. 安装脚本
    下载地址
git clone https://github.com/lagerspetz/linux-stuff
curl -O http://8.210.62.122/Linux/stuff/linux-stuff-master.zip
  1. 解压
unzip linux-stuff-master.zip
  1. 把脚本放到/bin目录下
mv linux-stuff-master/scripts/saferm.sh /bin
rm -rf linux-stuff-master  #清理环境  可选择执行
  1. 配置环境变量(单个用户)
cat /root/.bashrc
# .bashrc

# User specific aliases and functions

#alias rm='rm -i'
alias rm=saferm.sh
alias cp='cp -i'
alias mv='mv -i'

source /root/.bashrc #当前生效 or 退出当前shell 重新进入

测试

[root@localhost ~]# echo "123" > 123.txt
[root@localhost ~]# rm -rf 123.txt 
Moving 123.txt to /root/Trash
  1. 配置全局环境变量
[root@localhost ~]# tail -n 3 /etc/bashrc
fi
# vim:ts=4:sw=4
alias rm=saferm.sh

source /etc/bashrc 

测试

[root@localhost ~]# useradd ying
[root@localhost ~]# su - ying
[ying@localhost ~]$ pwd
/home/ying

echo "ying" >ying.txt
[ying@localhost ~]$ rm -rf ying.txt 
Moving ying.txt to /home/ying/Trash

当不想放在~/Trash目录时

[ying@localhost ~]$ echo "test" > test.txt
[ying@localhost ~]$ pwd
/home/ying
[ying@localhost ~]$ ls Trash/
ying.txt

[ying@localhost ~]$ \rm -rf Trash/ying.txt
[ying@localhost ~]$ ls Trash/  #为空
  1. 定时清理~/Trash目录下的文件
    脚本地址
[root@localhost scripts]# cat clean_Trash.sh 
#!/usr/bin/bash

#Tate:2022.3.29
#Author:Yingjian
#function: 定时清理~/Trash文件

#自定义清理用户目录
cat > home_list.txt << EOF
/root
/home/ying
EOF

while read line
do
find $line/Trash/ -mtime 7 -iname "*" -exec rm -rf {} \;
done < home_list.txt

0 2 * * * /bin/bash /data/scripts/clean_Trash.sh
Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐