Linux安裝資源回收筒功能的trash-cli 可以防止誤刪檔案

來源:互聯網
上載者:User

如何我們不小心在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]: 輸入對應序號還原


現在我們終於可以放心的刪檔案了,如果誤刪,就可以輕鬆找回來。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.