前幾天在rm編譯文件的時候不小心差點把主機rm了。。。。
所以想起小夥伴提過的回收站機制,每次執行rm 的時候實際上執行的是mv至某個特定文件夾
這樣的話避免一時手指敲錯無法挽回
參考http://www.manongjc.com/detail/10-kycmxtijgtgvxjb.html
Step1: 創建回收站目錄
在root下:
root@sf:~# mkdir .trash
Step2: 創建回收腳本
root@sf:~# vim .remove.sh
.remove.sh內容如下:
TRASH_DIR="$HOME/.trash/"
for i in $*; do
STAMP=`date +%Y%m%d`
FileName=`basename $i`
mv $i $TRASH_DIR/$FileName"_"$STAMP
done
Step3: 修改bashrc覆蓋rm命令
root@sf:~# vim .bashrc
在末尾加上:
alias rm='sh $HOME/.remove.sh'
測試下:
root@sf:~# touch testfile
root@sf:~# rm testfile
root@sf:~# ls .trash/
1111111_20190820 testfile_20190820
root@sf:~#
非root下相同操作執行一遍,這樣非root用戶也可以達到同樣效果