来,给Linux加个回收站吧!
刚开始接触Linux时,发现没有回收站功能,这货怎么能没有回收站呢?奇怪!用起来总有些怕怕的,特别是看到rm心里就要抖一抖 ,用久了之后呢,感觉rm上瘾啊,一天不rm几次总感觉不爽,但是仔细想想,万一哪天手贱来了个rm -rf / 那不就真的爽YY了,上回新闻报道那位错删公司服务器的哥们也不知怎么样了,rm会递归删除,伤害巨大,所以给linux系统弄个回收站功能还是很有必要的。 好,下面该我们的主角上场了。
trash-cli[作者git地址]
trash-cli是国外友人用python创造分享的一个小程序,他实现了类似回收站的功能,以后妈妈再也不用担心你误删了。
安装
别看是回收站,其实安装so easy:
[root@linux-node2 ~]# cd /tmp [root@linux-node2 tmp]# git clone https://github.com/andreafrancia/trash-cli [root@linux-node2 tmp]# cd trash-cli/ [root@linux-node2 trash-cli]# python setup.py install [root@linux-node2 trash-cli]# ll /usr/bin/ | grep trash -rwxr-xr-x 1 root root 123 Jun 6 18:53 trash -rwxr-xr-x 1 root root 125 Jun 6 18:53 trash-empty -rwxr-xr-x 1 root root 124 Jun 6 18:53 trash-list -rwxr-xr-x 1 root root 123 Jun 6 18:53 trash-put -rwxr-xr-x 1 root root 127 Jun 6 18:53 trash-restore -rwxr-xr-x 1 root root 122 Jun 6 18:53 trash-rm
功能说明
trash == trash-put == 删除,扔到垃圾桶去 trash-empty 清空 trash-list 列出回收站 trash-restore 恢复文件 trash-rm 删除回收站中指定文件
使用
安装好了,首先我们可以把系统删除命令替换,但是如果用trash完全代替rm会有些不方便,比如不记得trash命令啦, 比如我们真正要用rm -rf的时候怎么办呢?在README.rst使用说明文档中,作者建议我们保留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' 做了别名配置,使用rm删除时就会出现警告 [root@localhost ~]# touch test1 test2 test3 [root@localhost ~]# rm test1 This is not the command you are looking for.If you really want use rm simply prepend a slash 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@localhost ~]# \rm -rf test1 #不会提示直接干掉了
别名配置
[root@localhost ~]# sed -n '88,90p' /etc/bashrc alias rl='trash-list' alias rb='trash-restore' alias rm='echo " This is not the command you are looking for.If you really want use rm simply prepend a slash"; false' 编辑完别忘了source /etc/bashrc生效
我们现在来测试测试:CentOS release 6.7 (Final) x86_64
root@localhost ~]# touch test1 test2 test3 [root@localhost ~]# rm test1 #rm命令做了设置,不能直接删除 This is not the command you are looking for.If you really want use rm simply prepend a slash [root@localhost ~]# \rm -rf test1 #确定要删除用\rm [root@localhost ~]# trash test2 #用trash命令代替rm来干活 [root@localhost ~]# rl #查看回收站,rm直接删除没有test1了,回收站只有test2 2016-06-06 07:33:23 /root/test2 [root@localhost ~]# ll test2 ls: 无法访问test2: 没有那个文件或目录 [root@localhost ~]# trash test3 [root@localhost ~]# rl 2016-06-06 08:05:13 /root/test3 2016-06-06 07:33:23 /root/test2 [root@localhost ~]# trash-restore #还原 0 2016-05-06 11:49:59 /root/iptables.conf 1 2016-06-06 08:05:13 /root/test3 2 2016-06-06 07:33:23 /root/test2 What file to restore [0..2]: 1 [root@localhost ~]# trash-restore 0 2016-05-06 11:49:59 /root/iptables.conf 1 2016-06-06 07:33:23 /root/test2 What file to restore [0..1]: 1 [root@localhost ~]# ll test2 test3 -rw-r--r-- 1 root root 0 6月 6 07:30 test2 -rw-r--r-- 1 root root 0 6月 6 07:30 test3 [root@localhost ~]# trash test2 test3 #批量删除 [root@localhost ~]# ll test2 test3 ls: 无法访问test2: 没有那个文件或目录 ls: 无法访问test3: 没有那个文件或目录 [root@localhost ~]# rl 2016-05-06 11:49:59 /root/iptables.conf 2016-05-06 05:19:54 /tmp/b 2016-06-06 08:17:31 /root/test3 2016-06-06 08:17:31 /root/test2 2016-05-06 05:18:45 /tmp/a [root@localhost ~]# trash-empty 10 #清空10天前删除的文件 [root@localhost ~]# rl 2016-06-06 08:17:31 /root/test3 2016-06-06 08:17:31 /root/test2 [root@localhost ~]# trash-empty #清空回收站,再见! [root@localhost ~]# rl
问题
1.还原命令加别名会出错,用原命令就没问题,不会Python是硬伤啊 alias rb='trash-restore' [root@localhost ~]# rb rb waiting to receive.CCCCCCCCCCC 2.用trash可以同时删除多个,但不能同时批量还原 [root@localhost ~]# trash-restore 0 2016-05-06 11:49:59 /root/iptables.conf 1 2016-06-06 08:05:13 /root/test3 2 2016-06-06 07:33:23 /root/test2 What file to restore [0..2]: 1-2 Invalid entry What file to restore [0..2]: 1.2 Invalid entry 有能解决这两个问题的小伙伴可以教教我啊!
总结
总体来说,trash-cli真的是很强大是不是,快快感谢原作者吧,阿门!