使用trash-cli给Linux服务器加一个回收站的功能

1,安装。

1,安装方法一

安装非常简单,使用两条命令搞定。

yum install -y python-setuptools.noarch
easy_install trash-cli

一般情况下,没啥问题就安装成功了。

2,安装方法二

如果主机内核还是 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/

2,了解功能

安装之后,系统会新增几个工具:

[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           删除回收站中的单个文件

3,添加 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.

但是我觉得这样也是可以的,配置方便,已用。

4,使用示例

配置完成之后,我们可以正常的使用 rm 命令。

1,创建示例文件

[root@docker opt]$touch a b c
[root@docker opt]$mkdir 1 2 3
[root@docker opt]$ls
1  2  3  a  b  c

2,将其一并删除

[root@docker opt]$rm -rf ./*
[root@docker opt]$ls

3,使用命令查看

[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

4,恢复文件

[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

通过每个文件前边的数字选择,从而判断恢复哪个文件,目前貌似没发现批量恢复的办法,当然你可以直接去目录下进行恢复。

5,删除回收站的单个文件

[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

6,清空回收站

[root@docker opt]$trash-empty
[root@docker opt]$trash-list
[root@docker opt]$

基本上用法就是这些用法,如果是普通用户,那么道理是一样的,这里的道理一样指的是普通用户使用此命令之后,也会在其家目录下生成一个回收站。

通常在配置了回收站机制之后,都要顺手将自动清空回收站的机制加上的,不然会导致一些文件没被及时清除,从而让系统磁盘压力过大。

前几天在服务器增加了回收站功能,所以磁盘空间可能会受到影响,因此就再做一个策略就是自动清理回收站几天前的东西。

现有一个工具是 autotrash。

autotrash

自动清理工具

5,安装

可以直接从 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下了。

6,用法

$ autotrash -d 30   #删除回收站中超过 30 天的文件
$ autotrash -td 30  #删除所有用户的回收站文件,上边的命令只删除当前用户对应的回收站文件
$ autotrash --max-free 1024 -d 30  #如果回收站的剩余的空间少于 1GB,那么 autotrash 将从回收站中清除超过 30 天的已删除文件

7,配置

现实中我们将之写入到定时任务,从而让清理工作自动执行。

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 天前的文件了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CN-FuWei

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值