使用'df -h'检查特定文件夹的剩余磁盘空间百分比

时间:2017-08-14 03:53:16

标签: bash shell df

我正在使用' df -h '命令获取我目录中的磁盘空间详细信息,它给出了如下响应:

enter image description here

现在我希望能够通过一些批处理或脚本自动执行此检查 - 所以我想知道,如果我能够检查磁盘空间仅用于我关心的特定文件夹,如图所示 - 我是只应检查 / nas / home 它是否超过75%。

我怎样才能做到这一点?有什么帮助吗?

我的工作到现在为止:

我正在使用

df -h > DiskData.txt

...这会输出到文本文件

grep "/nas/home" "DiskData.txt"

...这给了我输出:

*500G  254G  247G  51% /nas/home*

现在我希望能够搜索前面或右边附近的'%'符号(本例中为51),以达到我想要的效果。

3 个答案:

答案 0 :(得分:1)

此命令将为您提供/ nas / home目录的百分比

df /nas/home | awk '{ print $4 }' | tail -n 1| cut -d'%' -f1

所以基本上你可以在某个变量中使用store作为值,然后在else条件下应用。

var=`df /nas/home | awk '{ print $4 }' | tail -n 1| cut -d'%' -f1`
if(var>75){
#send email
}

答案 1 :(得分:1)

另一种变体:

df --output=pcent /nas/home | tail -n 1 | tr -d '[:space:]|%'

输出= pcent - 仅显示百分比值(对于coreutils => 8.21)

答案 2 :(得分:1)

A more concise way without extensive piping could be:

df -h /nas/home | perl -ane 'print substr $F[3],0,-1 if $.==2'

Returns: 51 for your example.