为了防止误操作导致删除重要文件,特写以下代码
alias rm="/bin/sh /bin/denyrm.sh"
vim /bin/denyrm.sh
#/!bin/bash
#防止rm重要目录
#当同时使用-f和-r时,才生效
#可使用killit强制删除,否则移至/tmp
#writted by xly
if [ $# -eq 0 ];then
echo >&2 'rm:' $"missing file arguments"
echo >&2 $"Try \`rm --help' for more information."
exit 1
fi
force=
recursive=
killit=
while test $# -gt 0; do
case "$1" in
--h | --he | --hel | --help)
echo $"Usage: rm [OPTION]... FILE...
Remove (unlink) the FILE(s).
-f, --force ignore nonexistent files, never prompt
-i, --interactive prompt before any removal
--no-preserve-root do not treat \`/' specially (the default)
--preserve-root fail to operate recursively on \`/'
-r, -R, --recursive remove directories and their contents recursively
-v, --verbose explain what is being done
--killit igonre the limit, remove it now
--help display this help and exit
--version output version information and exit
By default, rm does not remove directories. Use the --recursive (-r or -R)
option to remove each listed directory, too, along with all of its contents.
To remove a file whose name starts with a \`-', for example \`-foo',
use one of these commands:
rm -- -foo
rm ./-foo
Note that if you use rm to remove a file, it is usually possible to recover
the contents of that file. If you want more assurance that the contents are
truly unrecoverable, consider using shred.
Report bugs to <bug-coreutils@gnu.org>."
exit 0
;;
*)
break
;;
esac
done
#can not rm
function rm_no {
dir=$1
must=( "/" "/bin" "/boot" "/data" "/dev" "/dist" "/etc" "/home" "/lib" "/lost+found" "/media" "/misc" "/mnt" "/net" "/opt" "/proc" "/root" "/sbin" "/selinux" "/srv" "/sys" "/tmp" "/usr" "/var" "/usr/local/db1" "/usr/local/db2")
#the dir or it's pdir in the array ,can not rm
if [ -z "$dir" ];then
dir_tmp1=`pwd`
elif [ -d "$dir" ];then
dir_tmp1=`cd $dir&&pwd`
fi
dir_tmp2=`cd ${dir_tmp1}/../&&pwd`
for((i=0;i<=${#must[*]};i++))
do
if [ "$dir_tmp1" == "${must[i]}" -o "$dir_tmp2" == "${must[i]}" ];then
echo " can't rm $dir "
exit 1;
fi
done
#mv
tmp3=`echo $dir_tmp1|awk -F'/' '{print $NF}'|tr -d '/'`
if [ -f /tmp/$tmp3 -o -d /tmp/$tmp3 ];then
mv /tmp/$tmp3 /tmp/${tmp3}_`date +%F%T`
mv $dir /tmp/
else
mv $dir /tmp/
fi
}
#rm anyway
function rm_yes {
rm -fr $1
exit 0
}
#-fr
tmp=`echo $* |awk -F' ' '{for(i=1;i<=NF;i++){if($i~/^-[a-z]/){print $i}}}'`
for i in $tmp
do
tmp1=`echo $i|sed -r 's/-[f,i,r,v,R]*//'`
if [ -z $tmp1 -a `echo $i|grep -e 'r\|R'` ];then
recursive=yes
fi
if [ -z $tmp1 -a `echo $i|grep 'f'` ];then
force=yes
fi
done
#--killit
tmp=`echo $* |grep ' \-\-killit'`
if [ -z "$tmp" ];then
killit=
else
killit=yes;
fi
#main
for file in `echo "$*"`
do
if [ -z `echo $file|sed -r 's/-[f,i,r,v,P]*//'` ];then
continue;
fi
if [ "$force" == "yes" -a "$recursive" == "yes" -a -z "$killit" ];then
rm_no $file
elif [ "$force" == "yes" -a "$recursive" == "yes" -a "$killit" == "yes" ];then
rm_yes $file
else
rm -i $*
fi
done