how to disable accidental deletion for some directories怎么避免意外误删除目录?
1. 下载safe-rm
社区有个safe-rm,可以解决绝大多数误删除目录的问题,安装命令:
apt install safe-rm
2. 配置safe-rm
默认safe-rm会禁止删除一些已知的系统目录,用户可以通过配置/etc/safe-rm.conf来增加额外的删除黑名单,下面例子里面后面2行配置都是我增加的调用删除时会提示skipping:
root@sse-shared-dev-011:/home# cat /etc/safe-rm.conf / /bin /boot /dev /etc /home /initrd /lib /proc /root /sbin /sys /usr /usr/bin /usr/include /usr/lib /usr/local /usr/local/bin /usr/local/include /usr/local/sbin /usr/local/share /usr/sbin /usr/share /usr/src /var /home/ronghua.zhou/test /home/test root@sse-shared-dev-011:/home# rm -fr /home/test/ safe-rm: skipping /home/test/
3. 增强safe-rm
但默认的代码还不够好,例如下面的命令可以正常运行:
rm -fr /home/test/* cd /home/ronghua.zhou/ rm -fr test/
需要手工修改一下:
--- safe-rm 2014-11-03 02:11:18.000000000 +0000 +++ safe-rm 2021-10-28 06:22:59.930180532 +0000 @@ -3,7 +3,6 @@ use warnings; use strict; use Cwd 'realpath'; - our $VERSION = '0.12'; my $homedir = $ENV{HOME} || q{}; @@ -11,6 +10,10 @@ my $USER_CONFIG_FILE = ($ENV{XDG_CONFIG_HOME} || "$homedir/.config") . "/safe-rm"; my $GLOBAL_CONFIG_FILE = '/etc/safe-rm.conf'; +sub trimstar { + my $s = shift; $s =~ s/\/\*?$//g; return $s +} + my %default_protected_dirs = ( '/bin' => 1, '/boot' => 1, @@ -76,7 +79,7 @@ my $pathname = $_; # Normalize the pathname - my $normalized_pathname = $pathname; + my $normalized_pathname = trimstar($pathname); if ( $normalized_pathname =~ m{/}xms or -e "$normalized_pathname" ) { # Convert to an absolute path (e.g. remove "..")
完整的代码,也可以在我的github仓库下载:
safe-rm/safe-rm at master · zhouronghua/safe-rm (github.com)