百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>服务器> Linux安装回收站功能的trash-cli 可以防止误删文件
分享文章到:

Linux安装回收站功能的trash-cli 可以防止误删文件

发布时间:01/15 来源: 浏览: 关键词:
Linux下也有类似于windows下的回收站功能,就是trash-cli,他可以让我们误删的文件还原,这样可以解决我们很多大问题的。

如何我们不小心在Linux系统中执行了以下命令,

rm -rf /

如果我们误删了文件,如何恢复呢?

这个时候,我们今天的主角 trash-cli 就要出场了,他实现了类似windows中回收站的功能。

trash-cli

trash-cli的项目地址 trash-cli https://github.com/andreafrancia/trash-cli

I. 安装

trash-cli的安装很简单

cd /tmp
https://github.com/andreafrancia/trash-cli
cd trash-cli
sudo python setup.py install

安装成功后我们的系统就有了以下工具

➜  ~  ll /usr/bin/|grep trash
-rwxr-xr-x   1 root root     123  5月 28 19:38 trash
-rwxr-xr-x   1 root root     125  5月 28 19:38 trash-empty
-rwxr-xr-x   1 root root     124  5月 28 19:38 trash-list
-rwxr-xr-x   1 root root     123  5月 28 19:38 trash-put
-rwxr-xr-x   1 root root     127  5月 28 19:38 trash-restore
-rwxr-xr-x   1 root root     122  5月 28 19:38 trash-rm

功能说明

trash == trash-put == 删除
trash-empty 清空
trash-list 列出回收站
trash-restore 恢复文件
trash-rm  删除回收站中指定文件

II. 如何使用
替换命令

为了安全着想,我们先将rm命令替换为trash

vim /etc/bashrc

添加以下内容,替换系统删除命令。

根据作者的README文件,建议还是轻易不要把rm做替换,下面的替换作废。
alias log='cd /var/log' # 安全删除 alias rm='trash' # 列出回收站 alias rl='trash-list'

作者原文

    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前确认
alias rm='echo " This is not the command you are looking for.If you really want use rm simply prepend a slash"; false'

以后执行rm的话,就会出现以下情况

➜  ~  rm
 This is not the command you are looking for.If you really want use rm simply prepend a slash

以后如果真的要删除的话,请使用\rm 真的很没用的文件,而使用trash 需要删除的文件。

完成后输入source /etc/bashrc是修改的别名生效。
常用操作

# 安全删除
rm ./*

# 查看回收站
➜  /tmp  rl
2015-05-28 19:59:54 /tmp/redis-stable

# 清空回收站
trash-empty

# 清空10天以前放入回收站的文件
trash-empty 10

# 还原文件
➜  /tmp  trash-restore
   0 2015-05-28 19:59:54 /tmp/redis-stable
What file to restore [0..0]: 输入对应序号还原


现在我们终于可以放心的删文件了,如果误删,就可以轻松找回来。

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有7人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板