rm两点建议

看到下厨房(http://tech.xiachufang.com/?p=18)因为rm把mysql的数据文件删除,这里不介绍如何恢复数据文件,对于rm我们要有敬畏之心,那我们可以对rm做些什么,防止误删呢?

1、建议为rm创建别名,对rm加-i –preserve-root

$which rm
alias rm='rm -i --preserve-root'
/bin/rm
 
$ rm rm.txt
rm:是否删除普通空文件 "rm.txt"?y
$ rm -r /
rm: 在"/" 进行递归操作十分危险
rm: 使用 --no-preserve-root 选项跳过安全模式

2、safe-rm使用麻烦一点,为了-f破坏也是值得的
可到下载http://code.google.com/p/safe-rm/。目前该工具还在完善中,有人维护,关键是用perl写的,激动

使用方法源码中:

safe-rm is meant to replace the rm command so you can achieve this by
putting a symbolic link with the name "rm" in a directory which sits
at the front of your path. For example, given this path:
 
PATH=/usr/local/bin:/bin:/usr/bin
 
You could create the following symbolic link:
 
ln -s /usr/local/bin/safe-rm /usr/local/bin/rm
 
Protected paths can be set both at the site and user levels.
 
Both of these configuration files can contain a list of important files
or directories (one per line):
 
/etc/safe-rm.conf
~/.safe-rm

环境:safe-rm放到/usr/local/bin/,并创建safe-rm软链接为rm,真正的rm在/bin下
注意:safe-rm命令路径一定要在真正rm路径之前,才会使用safe-rm。

有人会说为什么不把真正的rm删除呢?

$ sudo mv /bin/rm /bin/rm.bak
$ sudo rm -rf /tmp
Can't exec "/bin/rm": 没有那个文件或目录 at /usr/local/bin/rm line 113.

编辑/etc/safe-rm.conf

/tmp
/tmp/aa

注意:文件夹要以”/”结尾,不然起不到安全删除。eg:”/tmp”不能配置成”/tmp/”

创建两个文件

$ touch /tmp/aa
$ touch /tmp/ab

尝试删除文件夹及文件

$ sudo rm -rf /tmp
safe-rm: skipping /tmp
$ sudo rm -rf /tmp/aa
safe-rm: skipping /tmp/aa
$ sudo rm -rf /tmp/bb
$ ls
aa bb
$ ls
aa

Post a Comment