脚本
指定监控目录,指定空间使用百分比的阈值,当超过时,再删除。根据经验,一般超过 85%~90%时即可删除。该脚本可使用定时任务执行,根据磁盘使用空间,可半天或一天执行一次。
#!/bin/sh
#前提:已挂载目录
mount_dir=/mnt/hgfs
percent_in=85
file_del=1000
count_del=10
# 如果不存在目录,退出
if [ ! -d $mount_dir ]; then
echo $mount_dir "not found, quit"
exit
fi
percent=`df -h | grep $mount_dir | awk '{print $5}' | tr -d '%'`
dev_file=`df -h | grep $mount_dir | awk '{print $1}'`
file_count=`ls -l $mount_dir | wc -l`
echo $percent $percent_in
# 当空间占用百分比大于某个指定值时,删除目录前指定的数量
if [ $percent -ge $percent_in ];then
echo "need to remove file! occupy" $percent"%" "of" $dev_file
#cd $mount_dir
#file=`ls | sort | head -$file_del`
#rm $file
#cd -
else
echo "no need to remove file"
fi
# 按文件数量判断,用于文件体积小但数量大的情况,因其会占用文件索引
#if [ $file_count -ge $count_del ];then
# echo "need to remove file! occupy total" $count_del "files of" $dev_file
#cd $mount_dir
#file=`ls | sort | head -$file_del`
#rm $file
#cd -
#else
# echo "no need to remove file"
#fi
#file=`ls | sort | head -$file_del`
#echo $file
echo "comand complete at"
date
echo "======================================"