Linux检测硬盘空间不足则删除某些大文件

执行 df / 可以获取磁盘使用情况:

root@iZwz926bomo449tpd2nw49Z:~# df /
Filesystem     1K-blocks     Used Available Use% Mounted on
/dev/vda1       41151808 26778624  12259756  69% /

用awk取出数值那一列

root@iZwz926bomo449tpd2nw49Z:~# df / | awk '{print $5}'
Use%
69%

用grep取出纯数值:

root@iZwz926bomo449tpd2nw49Z:~# df / | awk '{print $5}' | grep -Eo '[0-9]+'
69

然后找出某个目录下超过10M的文件并删除。

总的脚本:

#!/bin/sh

#get disk usage percent
p=`df / | awk '{print $5}' | grep -Eo '[0-9]+' `

$if bigger than 85%,find log file bigger than 10M and delete 

if p > 85; then
  find /[your_log] -type f -size +10000k -delete
fi 


你可能感兴趣的:(ubuntu)