简体   繁体   English

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

[英]Use 'df -h' to check % remaining disk space of a specific folder

I am using ' df -h ' command to get disk space details in my directory and it gives me response as below : 我正在使用' df -h '命令获取我的目录中的磁盘空间详细信息,它给出了如下响应:

在此输入图像描述

Now I want to be able to do this check automatically through some batch or script - so I am wondering, if I will be able to check disk space only for specific folders which I care about, as shown in image - I am only supposed to check for /nas/home that it does not go above 75%. 现在我希望能够通过一些批处理或脚本自动执行此检查 - 所以我想知道,如果我能够检查磁盘空间仅用于我关心的特定文件夹,如图所示 - 我只应该检查/ nas / home是否超过75%。

How can I achieve this ? 我怎样才能做到这一点? Any help ? 有帮助吗?


My work till now: 我的工作到现在为止:

I am using 我在用

df -h > DiskData.txt

... this outputs to a text file ...输出到文本文件

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

... which gives me the output: ...给我输出:

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

Now I want to be able to search for the number previous or right nearby '%' sign (51 in this case) to achieve what I want. 现在我希望能够搜索前面或右边的'%'符号(在这种情况下为51)以实现我想要的数字。

This command will give you percentage of /nas/home directory 此命令将为您提供/ nas / home目录的百分比

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

So basically you can use store as value in some variable and then apply if else condition. 所以基本上你可以在某个变量中使用store作为值,然后在else条件下应用。

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

another variant: 另一个变种:

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

output=pcent - show only percent value (for coreutils => 8.21 ) output = pcent - 仅显示百分比值(对于coreutils => 8.21)

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. 返回: 51为您的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM