linux下的小工具--trash-rm

在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  闲人草堂 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/jtf-china/archive/2011/05/27/2059872.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值