大家还记得这个表情吗??
没错!懂一点Linux 基础的都明白这个命令! rm -rf 。 这个命令用不好的话,杀伤力奇高!
著名的 rm -rf 惨案!
2015年5月28日 携程疑似由于员工错误操作导致服务挂了
2017年2月2日 GitLab 管理员误删生产数据库
2017年4月5日 DigitalOcean 出现删除生产数据库的事故
2018年4月24日 Kuriko宣布 因机房技术人员 rm -rf /* ,导致 host 上所有数据丢失了
……
就在前一段时间博主竟然也踩坑了!!!!
线上环境发现某个目录下存在一些类似命名的<xxxxxx>.png 文件!于是乎想手动删掉。
咔咔输入以下命令 😎
rm -rf *>.png
回车~😎
😀过去0.5 秒
😉过去 1秒
😲 过去1.5秒
😥 过去 2秒
😱 我擦,赶紧 Ctrl + C
赶紧看看目录情况 ls
😭 被删了将近一半的文件
我去!这怎么办?该目录文件基本上都是代码,备份还是6个月之前的,当初偷懒还直接在线上改过这个目录代码!!!!
跑路吧!!!
😂,说笑了,跑路不存在的,就算跑路也得修复这个问题呀!经过2天终于解决了。
为了防止这种事情发生!尤其是线上服务器!是时候给他增加一个回收站功能了!
开源项目
网上介绍的方式很多,这个给大家推荐一个开源的项目!
https://github.com/andreafrancia/trash-cli/
部署安装
简单方法
要求:
Python 3 (Python 2.7 也可以)
pip (在 Debian 上用 apt-get install python-pip 来安装 pip)
安装命令:
1
|
pip install trash-cli
|
源码安装
为所有用户安装:
1
2
3
|
git clonehttps://github.com/andreafrancia/trash-cli.git
cd trash-cli
sudo pip install.
|
为当前用户安装:
1
2
3
|
git clonehttps://github.com/andreafrancia/trash-cli.git
cd trash-cli
pip install.
|
为当前用户安装后你可能需要把以下代码添加到 .bashrc:
1
|
export PATH=~/.local/bin:"$PATH"
|
卸载命令:
1
|
pip uninstall trash-cli
|
使用方法
1
2
3
4
5
|
trash-put 把文件或目录移动到回收站
trash-empty 清空回收站
trash-list 列出回收站文件
trash-restore 恢复回收站文件
trash-rm 删除回收站文件
|
移动文件到回收站:
1
|
$trash-put
|
列出回收站文件:
1
2
3
4
|
$trash-list
2008-06-0110:30:48/home/andrea/bar
2008-06-0221:50:41/home/andrea/bar
2008-06-2321:50:49/home/andrea/foo
|
搜索回收站文件:
1
2
3
|
$trash-list|grep foo
2007-08-3012:36:00/home/andrea/foo
2007-08-3012:39:41/home/andrea/foo
|
恢复回收站文件:
1
2
3
4
5
6
7
8
9
|
$trash-restore
02007-08-3012:36:00/home/andrea/foo
12007-08-3012:39:41/home/andrea/bar
22007-08-3012:39:41/home/andrea/bar2
32007-08-3012:39:41/home/andrea/foo2
42007-08-3012:39:41/home/andrea/foo
What file torestore[0..4]:4
$ls foo
foo
|
删除所有回收站文件:
1
|
$trash-empty
|
删除回收站中 n 天前被回收的文件:
1
|
$trash-empty<days>
|
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
$date
Tue Feb1920:26:52CET2008
$trash-list
2008-02-1920:11:34/home/einar/today
2008-02-1820:11:34/home/einar/yesterday
2008-02-1020:11:34/home/einar/last_week
$trash-empty7
$trash-list
2008-02-1920:11:34/home/einar/today
2008-02-1820:11:34/home/einar/yesterday
$trash-empty1
$trash-list
2008-02-1920:11:34/home/einar/today
|
只删除符合某种模式的文件:
1
|
$trash-rm\*.o
|
注意:要用双引号圈住模式来避免 shell 拓展。
定时清空回收站,可以利用crontab来实现
下面命令每天0点清空回收站3天前的内容
1
|
00***trash-empty3
|
常见问题
如何创建顶级 .Trash 目录?
步骤:
1
2
3
|
sudo mkdir--parent/.Trash
sudo chmoda+rw/.Trash
sudo chmod+t/.Trash
|
我能把 rm 的别名设置为 trash-put 吗?
可以,但不应该这样做。以前我觉得这是个好主意,但现在我不觉得。
虽然 trash-put 的界面看起来与 rm 兼容,但它们有不同的语法,这些差异会导致一些问题。比如,用 rm 删除目录时需要 -R,trash-put 则不需要。
但有时候我忘记用 trash-put 了,真的不能给 rm 设置别名吗?
你可以给 rm 设置一个别名来提醒你不要使用它:
1
|
alias rm='echo "This is not the command you are looking for."; false'
|
如果你真的要用 rm,那就在 rm 前加上斜杠来取消别名:
1
|
\rm file-without-hope
|
注意,Bash 别名是有在交互式界面才有效,所以使用这个别名不会影响使用 rm 的脚本。
被移动到回收站的文件在哪?
从 home 分区移动到回收站的文件在这:
1
|
~/.local/share/Trash/
|
出现错误
马甲 CentOS7 安装时出现了一个错误
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
Processing/root/trash-cli
Collecting psutil(from trash-cli==0.21.7.24)
Using cached https://pypi.tuna.tsinghua.edu.cn/packages/e1/b0/7276de53321c12981717490516b7e612364f2cb372ee8901bd4a66a000d7/psutil-5.8.0.tar.gz
Complete output from command python setup.py egg_info:
/usr/lib64/python2.7/distutils/dist.py:267:UserWarning:Unknown distribution option:'python_requires'
warnings.warn(msg)
/usr/lib64/python2.7/distutils/dist.py:267:UserWarning:Unknown distribution option:'long_description_content_type'
warnings.warn(msg)
error inpsutil setup command:'extras_require'must beadictionary whose values are strings orlists of strings containing valid project/version requirement specifiers.
----------------------------------------
Command"python setup.py egg_info"failed with error code1in/tmp/pip-build-n6gexX/psutil/
You are using pip version8.1.2,however version21.2.3isavailable.
You should consider upgrading via the'pip install --upgrade pip'command.
|
后来发现 psutil 版本的问题,执行安装旧版本即可!
1
|
pip install psutil==5.7.0
|
最后说明
当然还有好多其他的解决方案。比如用alias !
1
|
alias rm='rm -i'
|
下面是我目前使用的alias,希望对大家有所帮助吧!
1
2
3
4
5
6
7
8
9
10
11
|
alias rm='echo "This is not the command you are looking for."; false'
alias ls='ls --show-control-chars -F --color=auto'
alias ll='ls -hl'
alias la='ls -a'
alias cp='cp -i'
alias mv='mv -i'
alias less='less -R'
alias cat='ccat'
alias diff='colordiff'
alias grep='grep --color=auto'
alias egrep='egrep --color=auto'
|