how to disable accidental deletion for some directories怎麼避免意外誤刪除目錄?
阿新 • • 發佈:2021-10-28
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)