当前位置: 技术问答>linux和unix
想问一个shell有关的问题
来源: 互联网 发布时间:2016-09-07
本文导语: 想实现如下功能 当某个文件夹的大小,达到系统磁盘总量的90%时,就删除这个文件夹里的所有文件 用shell实现,麻烦各位了哈~ | #!/bin/bash testdir=DIR if [ ! -d ${testdir} ] then echo "no th...
想实现如下功能
当某个文件夹的大小,达到系统磁盘总量的90%时,就删除这个文件夹里的所有文件
用shell实现,麻烦各位了哈~
当某个文件夹的大小,达到系统磁盘总量的90%时,就删除这个文件夹里的所有文件
用shell实现,麻烦各位了哈~
|
#!/bin/bash
testdir=DIR
if [ ! -d ${testdir} ]
then
echo "no the dir "
fi
sizedir=`du -sh ${testdir} | awk -F 'G' '{print $1}'`
if [ ${sizedir} > 60 ]
then
cd ${testdir}
rm -rf *
fi
exit 0
完了,把这个shell加到crontab中就可以啦
testdir=DIR
if [ ! -d ${testdir} ]
then
echo "no the dir "
fi
sizedir=`du -sh ${testdir} | awk -F 'G' '{print $1}'`
if [ ${sizedir} > 60 ]
then
cd ${testdir}
rm -rf *
fi
exit 0
完了,把这个shell加到crontab中就可以啦
|
250G 就是超过225G就删除了?
试试我这个 把第一行/your/path改成实际path 千万小心哦
你可以先测试一下 先把rm -rf那一句换成一句普通的输出 比如echo "i will rm"
再改回去真正执行删除
#!/bin/sh
target_path=/your/path
dir_size=`du -skh $target_path | grep -o -E '([0-9]{1,})G' | cut -d'G' -f1`
if [ x$dir_size != x ] && [ $dir_size -gt 225 ];then
rm -rf $target_path
fi
|
不会吧 我这就好用啊 改改
#!/bin/sh
target_path=/your/path
dir_size=`du -skh $target_path | grep -o -E '([0-9]{1,})G' | cut -d'G' -f1`
if [ "$dir_size" != "" ];then
test $dir_size -gt 225 && rm -rf $target_path
fi
|
不知道你啥操作系统,我写个aix下面的shell你试试吧
入参为你需要检查的目录
#!/bin/ksh
path_name=$1
path_size=`du -sk $path_name | awk '{ print $1 }'`
echo "path size is : $path_size [k]"
path_sizeM=`expr $path_size / 1024`
echo "path size is : $path_sizeM [m]"
path_sizeG=`expr $path_sizeM / 1024`
echo "path size is : $path_sizeG [g]"
if [ $path_sizeG -gt 225 ]
then
rm -rf $1/*
echo "$1 file and path delete !"
else
echo "$1 check ok!"
fi;