rm -rf 又一起惨案,该给Linux整一个回收站了! - 如有乐享

rm -rf 又一起惨案,该给Linux整一个回收站了! - 如有乐享

如有乐享

 

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

 

没错!懂一点Linux 基础的都明白这个命令! rm -rf 。 这个命令用不好的话,杀伤力奇高!

著名的 rm -rf 惨案!

2015年5月28日 携程疑似由于员工错误操作导致服务挂了

2017年2月2日GitLab 管理员误删生产数据库

2017年4月5日 DigitalOcean 出现删除生产数据库的事故

2018年4月24日 Kuriko宣布 因机房技术人员rm -rf /* ,导致 host 上所有数据丢失了

……

就在前一段时间博主竟然也踩坑了!!!!

线上环境发现某个目录下存在一些类似命名的.png 文件!于是乎想手动删掉。

咔咔输入以下命令

rm -rf *>.png

回车~

过去0.5 秒

过去 1秒

过去1.5秒

过去 2秒

我擦,赶紧 Ctrl + C

赶紧看看目录情况 ls

被删了将近一半的文件

我去!这怎么办?文件的备份还是6个月之前的,当初偷懒还直接在线上改过代码!!!!跑路吧!!!

 

尤其是线上服务器!是时候给Linux 增加一个回收站功能了!

 

开源项目

https://github.com/andreafrancia/trash-cli/

 

部署安装

简单方法

要求:

Python 3 (Python 2.7 也可以)

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

安装命令:

pip install trash-cli

源码安装

为所有用户安装:

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

为当前用户安装:

git clone https://github.com/andreafrancia/trash-cli.gitcd trash-clipip install .

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

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

卸载命令:

pip uninstall trash-cli

使用方法

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

移动文件到回收站:

$ trash-put

列出回收站文件:

$ trash-list2008-06-01 10:30:48 /home/andrea/bar2008-06-02 21:50:41 /home/andrea/bar2008-06-23 21:50:49 /home/andrea/foo

搜索回收站文件:

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

恢复回收站文件:

$ trash-restore0 2007-08-30 12:36:00 /home/andrea/foo1 2007-08-30 12:39:41 /home/andrea/bar2 2007-08-30 12:39:41 /home/andrea/bar23 2007-08-30 12:39:41 /home/andrea/foo24 2007-08-30 12:39:41 /home/andrea/fooWhat file to restore [0..4]: 4$ ls foofoo

删除所有回收站文件:

$ trash-empty

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

$ trash-empty <days>

示例:

$ dateTue Feb 19 20:26:52 CET 2008$ trash-list2008-02-19 20:11:34 /home/einar/today2008-02-18 20:11:34 /home/einar/yesterday2008-02-10 20:11:34 /home/einar/last_week$ trash-empty 7$ trash-list2008-02-19 20:11:34 /home/einar/today2008-02-18 20:11:34 /home/einar/yesterday$ trash-empty 1$ trash-list2008-02-19 20:11:34 /home/einar/today

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

$ trash-rm \*.o

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

 

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

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

0 0 * * * trash-empty 3

 

常见问题

如何创建顶级 .Trash 目录?

步骤:

sudo mkdir --parent /.Trashsudo chmod a+rw /.Trashsudo 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-cliCollecting 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 rm='rm -i'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'

 

 

 

 

 


本文章由 flowerss 抓取自RSS,版权归源站点所有。

查看原文:rm -rf 又一起惨案,该给Linux整一个回收站了! - 如有乐享

Report Page