闲人草堂

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

在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-trash
list-trash
restore-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"!
posted on 2011-05-27 14:25  闲人草堂  阅读(2837)  评论(0编辑  收藏  举报