rm命令linux00,Linux限制rm命令

作者分享了一个自定义的 rm 命令别名,该脚本在删除文件时提供了安全提示,仅允许删除普通文件和目录,并将待删内容移动到指定的垃圾目录中,便于后续清理。通过 /etc/profile.d/ 部署,增加了删除操作的透明性和谨慎性。
摘要由CSDN通过智能技术生成

常规做法是新建一个垃圾目录,把删除的内容mv到这个目录,在定期清理这个目录。是一样的,我自己动基于alias写了个脚本来限制rm命令,有以下好处:

只有一个脚本批量部署非常方便,不需求安装第三方软件

每次删除时会提示删除的内容,删除是将要删除的文件mv到/tmp/.trash/当天日期下,同时会提示是否清理目录,相当于仔细检查

限制删除内容:只允许删除文件和目录,对于特殊的软链接,设备等文件可以临时删除

其他功能懒得写了,使用方法将脚本丢到/etc/profile.d/即可。

#!/bin/bash

#author: will

#website: https://www.52os.net

rm_cmd(){

arg_array=()

for var in $*

do

if [ ! `echo "$var" |grep "^-"` ];then

arg_array+=("$var")

fi

done

if [ "$#" -eq "0" ] ;then

echo -e "\e[00;32mYou Are Using Security \"rm\" command \e[00m"

return 0

elif [ ${#arg_array[@]} -eq 0 ];then

echo -e "\e[00;32mYou Are Using Security \"rm\" command \e[00m"

return 0

fi

echo -e "\033[00;31mYou are going to DELETE: \033[00m"

list_array=()

for element in ${arg_array[@]}

do

if [ -f $element ];then

echo FILE: $element

list_array+=("$element")

elif [ -d $element ];then

echo DIR: $element

list_array+=("$element")

elif [ -S $element ];then

echo -e "\e[00;32mSOCKET: $element NOT Allow To Delete\e[00m"

return 0

elif [ -p $element ];then

echo -e "\e[00;32mPIPE: $element NOT Allow To Delete\e[00m"

return 0

elif [ -b $element ];then

echo -e "\e[00;32mBLOCK DEVICE: $element NOT Allow To Delete\e[00m"

return 0

elif [ -c $element ];then

echo -e "\e[00;32mCHARACTER DEVICE: $element NOT Allow To Delete\e[00m"

return 0

else

echo -e "\e[00;32mNOT Exist: $element \e[00m"

return 0

fi

done

read -n1 -p $'\033[00;31mAre you sure to DELETE [Y/N]? ' answer

case $answer in

Y | y)

echo -e "\n"

if [ ! -d "/tmp/.trash/`date -I`" ]; then

mkdir -p /tmp/.trash/`date -I`

chmod 777 /tmp/.trash/`date -I`

fi

for element in ${list_array[@]}

do

echo -e "Deleting $element to /tmp/.trash/`date -I`"

#/bin/rm --preserve-root -rf $element

mv $element /tmp/.trash/`date -I`

if [ $? -ne "0" ];then

echo -e "\nDeleted FAILED"

return 0

fi

done

echo -e "\nDeleted FINISHED"

read -n1 -p $'\033[00;31mFree Disk Space ? [Y/N]? ' fanswer

case $fanswer in

Y | y)

/bin/rm --preserve-root -rf /tmp/.trash/*

echo -e "\n"

;;

*)

echo -e "\nFree Disk Space SKIPED"

echo -e "\n"

;;

esac

;;

*)

echo -e "\nDelete SKIPED"

;;

esac

}

alias rm='rm_cmd $@'

alias grep='grep --color=auto'

alias cp='cp -i'

alias l.='ls -d .* --color=auto'

alias ll='ls -l --color=auto'

alias ls='ls --color=auto'

alias mv='mv -i'

alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值