惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Apple Machine Learning Research
Apple Machine Learning Research
Recent Announcements
Recent Announcements
IT之家
IT之家
人人都是产品经理
人人都是产品经理
G
Google Developers Blog
Microsoft Azure Blog
Microsoft Azure Blog
博客园_首页
大猫的无限游戏
大猫的无限游戏
U
Unit 42
罗磊的独立博客
博客园 - Franky
WordPress大学
WordPress大学
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
M
MIT News - Artificial intelligence
SecWiki News
SecWiki News
V
Vulnerabilities – Threatpost
P
Privacy International News Feed
P
Palo Alto Networks Blog
F
Fortinet All Blogs
P
Proofpoint News Feed
博客园 - 叶小钗
C
CERT Recently Published Vulnerability Notes
T
Tor Project blog
Spread Privacy
Spread Privacy
S
Securelist
C
Cisco Blogs
I
Intezer
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Cyberwarzone
Cyberwarzone
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Privacy & Cybersecurity Law Blog
宝玉的分享
宝玉的分享
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Engineering at Meta
Engineering at Meta
S
Schneier on Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
GbyAI
GbyAI
T
Troy Hunt's Blog
T
Threatpost
博客园 - 司徒正美
Y
Y Combinator Blog
Hugging Face - Blog
Hugging Face - Blog
AWS News Blog
AWS News Blog
T
The Blog of Author Tim Ferriss
G
GRAHAM CLULEY
N
Netflix TechBlog - Medium
酷 壳 – CoolShell
酷 壳 – CoolShell
Google DeepMind News
Google DeepMind News
Know Your Adversary
Know Your Adversary
S
SegmentFault 最新的问题

博客园 - centosboy

Thinkpad T490卡顿排查 xinference初探 diff的安装与使用 ubuntu docker运行大模型 vllm部署 zabbix监控mongodb /root/.dbshell无权限解决办法 开机启动后应用limit没有生效 关于运维工作的一点感悟 将CDLINUX装入U盘 Autossh打洞 hive分区表实践 hashlib模块 linux 通过inode删除文件 那些年踩过的坑 htpdate代替ntpdate同步时间 Openfire搭建聊天系统 zookeeper运维 centos ping添加丢包记录 Linux tee命令输出显示在屏幕和保存到日志文件中
rm 防误删脚本
centosboy · 2020-12-15 · via 博客园 - centosboy

前记:
脚本称为《rm后悔药》吧,虽然多年运维,依旧无法避免误操作,既然无法避免,就做好误删准备。脚本参考之前转载的文章,进行了简单的完善。
脚本内容:

#创建回收站
mkdir -p /opt/apps/.recover/
chmod 777 /opt/apps/.recover
mkdir -p /opt/apps/shell/
#创建回收脚本
cat >/opt/apps/shell/remove.sh <<EOF
#!/bin/bash
RECOVER_DIR="/opt/apps/.recover/\$USER"
if [[ ! -d "/opt/apps/.recover/\$USER" ]]
then
    mkdir "/opt/apps/.recover/\$USER"
fi
for i in \$*; do
    STAMP=\`date +%s\`
    fileName=\`basename \$i\`
    mv \$i \$RECOVER_DIR/\$fileName.\$STAMP
done
EOF

chmod +x /opt/apps/shell/remove.sh

#增加环境变量
cat >>/etc/bashrc <<EOF
alias rm='sh /opt/apps/shell/remove.sh'
EOF

#设置rm别名,用于删除back文件
ln -s /usr/bin/rm /usr/bin/rm-rm

#设置凌晨清空文件
cat >>/etc/crontab <<EOF
0 3 * * * root rm -rf /opt/apps/.recover/*
EOF
#生效环境变量
source /etc/bashrc

简单说明下:
1.将要删除的文件mv至回收站,凌晨3点清空。
2.为什么要加入/etc/bashrc呢?因为用户交互式登陆系统或者交互式非登陆系统,/etc/bashrc都会执行,这样,系统所有用户都可以共享此功能。
3.将rm软连接rm-rm,有临时需要永久删除的文件,可以使用此命令立即生效
4.为什么计划任务可以使用rm呢?因为计划任务是非交互非登陆的,环境变量不生效