rm -rf 又一起惨案,该给Linux整一个回收站了!_git

前言:

大家还记得这个表情吗??

rm -rf 又一起惨案,该给Linux整一个回收站了!_git_02


没错!懂一点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​

回车~

rm -rf 又一起惨案,该给Linux整一个回收站了!_python_03过去0.5 秒

rm -rf 又一起惨案,该给Linux整一个回收站了!_github_04过去 1秒

rm -rf 又一起惨案,该给Linux整一个回收站了!_git_05 过去1.5秒

 rm -rf 又一起惨案,该给Linux整一个回收站了!_python_06过去 2秒

rm -rf 又一起惨案,该给Linux整一个回收站了!_python_07 我擦,赶紧 Ctrl + C

赶紧看看目录情况 ls

 rm -rf 又一起惨案,该给Linux整一个回收站了!_github_08被删了将近一半的文件

我去!这怎么办?该目录文件基本上都是代码,备份还是6个月之前的,当初偷懒还直接在线上改过这个目录代码!!!!跑路吧!!!rm -rf 又一起惨案,该给Linux整一个回收站了!_git_09说笑了,跑路不存在的,就算跑路也得修复这个问题呀!经过2天终于解决了。为了防止这种事情发生!尤其是线上服务器!是时候给他增加一个回收站功能了!


开源项目:

网上介绍的方式很多,这个给大家推荐一个开源的项目!

https://github.com/andreafrancia/trash-cli/">​https://github.com/andreafrancia/trash-cli/​

部署安装:

简单方法

要求:


Python 3 (Python 2.7 也可以)

pip (在 Debian 上用 apt-get install python-pip 来安装 pip)


安装命令:



pip install trash-cli

源码安装


为所有用户安装:

git clonehttps://github.com/andreafrancia/trash-cli.git
cd trash-cli
sudo pip install.

为当前用户安装:






https%3A%2F%2Fgithub.com%2Fandreafrancia%2Ftrash-cli.git%5Cncd%20trash-cli%5Cnpip%20install.%5Cn%5Cn%22%2C%22id%22%3A%22QlyP2%22%2C%22type%22%3A%22block%22%7D" data-card-type="block" data-card-key="codeblock" data-id="dbcf13f9-Qi7YZC8R" data-card-editable="false">
xxxxxxxxxx
 
1
git clonehttps://github.com/andreafrancia/trash-cli.git
2
cd trash-cli
3
pip install.
4
5

为当前用户安装后你可能需要把以下代码添加到 .bashrc:

git clone https://github.com/andreafrancia/trash-cli.git 
cd trash-cli
pip install .

为当前用户安装后你可能需要把以下代码添加到 .bashrc: 



export PATH=~/.local/bin:"$PATH"


卸载命令: 



pip uninstall trash-cli

使用方法:


trash-put           把文件或目录移动到回收站
trash-empty 清空回收站
trash-list 列出回收站文件
trash-restore 恢复回收站文件
trash-rm 删除回收站文件

移动文件到回收站:



$ trash-put

列出回收站文件:  


$ trash-list
2008-06-01 10:30:48 /home/andrea/bar
2008-06-02 21:50:41 /home/andrea/bar
2008-06-23 21:50:49 /home/andrea/foo

 搜索回收站文件:

$ trash-list | grep foo
2007-08-30 12:36:00 /home/andrea/foo
2007-08-30 12:39:41 /home/andrea/foo

恢复回收站文件:

$ trash-restore
0 2007-08-30 12:36:00 /home/andrea/foo
1 2007-08-30 12:39:41 /home/andrea/bar
2 2007-08-30 12:39:41 /home/andrea/bar2
3 2007-08-30 12:39:41 /home/andrea/foo2
4 2007-08-30 12:39:41 /home/andrea/foo
What file to restore [0..4]: 4
$ ls foo
foo

删除所有回收站文件:



$ trash-empty

删除回收站中 n 天前被回收的文件:



$ trash-empty <days>

示例:

$ date
Tue Feb 19 20:26:52 CET 2008
$ trash-list
2008-02-19 20:11:34 /home/einar/today
2008-02-18 20:11:34 /home/einar/yesterday
2008-02-10 20:11:34 /home/einar/last_week
$ trash-empty 7
$ trash-list
2008-02-19 20:11:34 /home/einar/today
2008-02-18 20:11:34 /home/einar/yesterday
$ trash-empty 1
$ trash-list
2008-02-19 20:11:34 /home/einar/today

只删除符合某种模式的文件:



$ trash-rm \*.o

注意:要用双引号圈住模式来避免 shell 拓展。


定时清空回收站,可以利用crontab来实现

下面命令每天0点清空回收站3天前的内容



0 0 * * * trash-empty 3

常见问题:


如何创建顶级 .Trash 目录?

步骤:

sudo mkdir --parent /.Trash
sudo chmod a+rw /.Trash
sudo chmod +t /.Trash

我能把 rm 的别名设置为 trash-put 吗?

可以,但不应该这样做。以前我觉得这是个好主意,但现在我不觉得。

虽然 trash-put 的界面看起来与 rm 兼容,但它们有不同的语法,这些差异会导致一些问题。比如,用 rm 删除目录时需要 -R,trash-put 则不需要。

但有时候我忘记用 trash-put 了,真的不能给 rm 设置别名吗?

你可以给 rm 设置一个别名来提醒你不要使用它:



alias rm='echo "This is not the command you are looking for."; false'

如果你真的要用 rm,那就在 rm 前加上斜杠来取消别名:



\rm file-without-hope

注意,Bash 别名是有在交互式界面才有效,所以使用这个别名不会影响使用 rm 的脚本。

被移动到回收站的文件在哪?

从 home 分区移动到回收站的文件在这:



~/.local/share/Trash/ 

出现错误:


马甲 CentOS7 安装时出现了一个错误

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 in psutil setup command: 'extras_require' must be a dictionary whose values are strings or lists of strings containing valid project/version requirement specifiers.


----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-n6gexX/psutil/
You are using pip version 8.1.2, however version 21.2.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

后来发现 psutil 版本的问题,执行安装旧版本即可!



pip install psutil==5.7.0

最后说明:


当然还有好多其他的解决方案。比如用alias !



alias rm='rm -i'

下面是我目前使用的alias,希望对大家有所帮助吧!

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'

福利时间:


感谢北大社一直一来对本号的支持和赞助,本期书籍适合想系统学习 docker 和 kubernetes,以及希望通过 CKA 和 CKAD 考试的读者学习使用。此外,本书中的许多案例还可以直接应用于生产环境。


rm -rf 又一起惨案,该给Linux整一个回收站了!_git_10