在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"!