linux下的小工具–trash-rm

來源:互聯網
上載者:User

在linux下用rm刪除檔案,一不小心就會幹壞事……譬如不小心把系統檔案刪光了,所以萌生一個想法,能不能把rm刪除動作綁定到垃圾箱,這樣萬一刪錯了還能找回來。

上網搜了一下,發現已經有人幹過這事,轉載過來供大家參考。

原帖在這 http://www.webupd8.org/2010/02/make-rm-move-files-to-trash-instead-of.html

1. Install trash-cli

This package provides a command line interface trashcan
utility compliant with the FreeDesktop.org Trash Specification. It
remembers the name, original path, deletion date, and permissions of
each trashed file.

In Ubuntu, simply run this command:

sudo apt-get install trash-cli


2. Set up the script

In Ubuntu enter this in a terminal:

sudo gedit /usr/local/bin/trash-rm


And paste this in the newly opened file, then save it:

 1 #!/bin/bash
2  # command name: trash-rm
3 shopt -s extglob
4 recursive=1
5 declare -a cmd
6 ((i = 0))
7 for f in "$@"
8 do
9 case "$f" in
10 (-*([fiIv])r*([fiIv])|-*([fiIv])R*([fiIv]))
11 tmp="${f//[rR]/}"
12 if [ -n "$tmp" ]
13 then
14 #echo "\$tmp == $tmp"
15 cmd[$i]="$tmp"
16 ((i++))
17 fi
18 recursive=0 ;;
19 (--recursive) recursive=0 ;;
20 (*)
21 if [ $recursive != 0 -a -d "$f" ]
22 then
23 echo "skipping directory: $f"
24 continue
25 else
26 cmd[$i]="$f"
27 ((i++))
28 fi ;;
29 esac
30 done
31 trash "${cmd[@]}"


Then make it executable by opening a terminal and running this:

sudo chmod +x /usr/local/bin/trash-rm


3. Create an alias for "rm" to use "trash-rm"

In Ubuntu, run this in a terminal:

gedit ~/.bashrc


and enter this at the end of the file:

alias rm="trash-rm"


and save it.

Then reload bashrc by running the following command in a terminal:

bash


That's it! Now try it out by deleting files the way you always do, using "rm" and "rm -r".

Final trash-cli tips

Since you've installed the trash-cli utility, now you can use the
following commands for manipulating the trash from the command line (the
names are self explanatory):

empty-trashlist-trashrestore-trash


Update: to get this to work for "sudo rm" as well, copy wilo108's sudo wrapper in your ~/.bashrc file. Without it, this will not work when "rm" is used with "sudo"!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.