博客 > 术业专攻> Linux基础> 系统与优化> 使用trash-cli给Linux服务器加一个回收站的功能 2019年08月29日 11:23:54
有些让人痛心的经历,会让你成长,我相信,所有开始使用这个工具,以及开始愿意去部署这个工具的人,应该都是有遭受过误删除所带给自己的那种恐慌以及无奈。
我也一样,所以,我打算了解一下回收站的事儿,就走进了开源工具trash-cli
。地址如下:
安装非常简单,使用两条命令搞定。
yum install -y python-setuptools.noarch easy_install trash-cli
一般情况下,没啥问题就安装成功了。
如果主机内核还是2.6的CentOS6,那么可以通过如下方式安装,这种安装方式,适用于`CentOS6`以及`CentOS7`:
wget https://github.com/andreafrancia/trash-cli/archive/master.zip unzip master.zip cd trash-cli-master python setup.py install
如果最后一步安装不报错,那就成功了。
有时候这种方式安装完成之后,可能无法在系统层面适用对应的命令,可以加入如下一条命令:
ln -s /usr/local/python/bin* /usr/bin/
即可正常使用!
安装之后,系统会新增几个工具:
[root@docker ~]$ls /usr/bin/ |grep trash trash trash-empty trash-list trash-put trash-restore trash-rm
说明:
trash-put 将文件或目录移入回收站 trash-empty 清空回收站 trash-list 列出回收站中的文件 trash-restore 还原回收站中的文件 trash-rm 删除回收站中的单个文件
直接在系统当中添加如下命令:
[root@docker ~]$echo "alias rm='trash-put'" >> /etc/bashrc [root@docker ~]$source /etc/bashrc
虽然在Github中作者并不建议这么做,而是建议当使用rm的时候输出一段提示,作者原话说:
Can I alias rm to trash-put? You can but you shouldn't. In the early days I thought it was good idea do that but now I changed my mind. The interface of trash-put seems to be compatible with rm it has a different semantic that will cause you problems. For example, while rm requires -R for deleting directories trash-put does not. But sometimes I forgot to use trash-put, really can't I? You may alias rm to something that will remind you to not use it: alias rm='echo "This is not the command you are looking for."; false' If you really want use rm simply prepend a slash: \rm file-without-hope Note that Bash aliases are used only in interactive shells, so using this alias should not interfere with scripts that expects to use rm.
但是我觉得这样也是可以的,配置方便,已用。
配置完成之后,我们可以正常的使用rm命令。
[root@docker opt]$touch a b c [root@docker opt]$mkdir 1 2 3 [root@docker opt]$ls 1 2 3 a b c
[root@docker opt]$rm -rf ./* [root@docker opt]$ls
[root@docker opt]$trash-list 2018-10-30 16:21:00 /opt/1 2018-10-30 16:21:00 /opt/2 2018-10-30 16:21:00 /opt/3 2018-10-30 16:21:00 /opt/a 2018-10-30 16:21:00 /opt/b 2018-10-30 16:21:00 /opt/c
这个时候,文件其实保存在$USER/.local/share/Trash/files
下,可以用命令查看一下。
[root@docker opt]$ls /$USER/.local/share/Trash/files 1 2 3 a b c
[root@docker opt]$trash-restore 0 2018-10-30 16:21:00 /opt/1 1 2018-10-30 16:21:00 /opt/2 2 2018-10-30 16:21:00 /opt/3 3 2018-10-30 16:21:00 /opt/a 4 2018-10-30 16:21:00 /opt/b 5 2018-10-30 16:21:00 /opt/c What file to restore [0..5]: 0 [root@docker opt]$ls 1
通过每个文件前边的数字选择,从而判断恢复哪个文件,目前貌似没发现批量恢复的办法,当然你可以直接去目录下进行恢复。
[root@docker opt]$trash-rm 2 [root@docker opt]$trash-list 2018-10-30 16:21:00 /opt/3 2018-10-30 16:21:00 /opt/a 2018-10-30 16:21:00 /opt/b 2018-10-30 16:21:00 /opt/c
[root@docker opt]$trash-empty [root@docker opt]$trash-list [root@docker opt]$
基本上用法就是这些用法,如果是普通用户,那么道理是一样的,这里的道理一样指的是普通用户使用此命令之后,也会在其家目录下生成一个回收站。
通常在配置了回收站机制之后,都要顺手将自动清空回收站的机制加上的,不然会导致一些文件没被及时清除,从而让系统磁盘压力过大。
前几天在服务器增加了回收站功能,所以磁盘空间可能会受到影响,因此就再做一个策略就是自动清理回收站几天前的东西。
现有一个工具是autotrash。
工具地址:https://github.com/bneijt/autotrash
可以直接从github下载:
$ wget https://github.com/bneijt/autotrash/archive/v0.2.1.zip $ unzip v0.2.1.zip $ cd autotrash-0.2.1 $ python setup.py install $ whereis autotrash autotrash: /usr/bin/autotrash
执行安装完成之后,对应的可执行文件就已经自动放入了/usr/bin
下了。
注意github上直接下载的这个可能会报错,那么我将可用的已经上传了百度云,可在下边下载。
地址如下:
文件下载 | 文件名称:autotrash | 文件大小:90k |
下载声明:本站文件大多来自于网络,仅供学习和研究使用,不得用于商业用途,如果有版权问题,请联系博主进行相关处理! | ||
下载地址:https://pan.baidu.com/s/1cQ2CKyK7OjSIOJMP7QwDGw |
提取码在下边,根据提示获取即可。
$ autotrash -d 30 #删除回收站中超过 30 天的文件 $ autotrash -td 30 #删除所有用户的回收站文件,上边的命令只删除当前用户对应的回收站文件 $ autotrash --max-free 1024 -d 30 #如果回收站的剩余的空间少于 1GB,那么 autotrash 将从回收站中清除超过 30 天的已删除文件
现实中我们将之写入到定时任务,从而让清理工作自动执行。
echo "#add clean tool" >> /var/spool/cron/root echo "@daily /usr/bin/autotrash -d 7" >> /var/spool/cron/root echo "#add clean tool" >> /var/spool/cron/test echo "@daily /usr/bin/autotrash -d 7" >> /var/spool/cron/test
这样就实现了每天清理回收站7天前的文件了。
© 2018 www.qingketang.net 鄂ICP备18027844号-1
武汉快勤科技有限公司 13554402156 武汉市东湖新技术开发区关山二路特一号国际企业中心6幢4层7号
扫码关注,全站教程免费播放
订单金额:
支付金额:
支付方式: