dyn*_*Jos 1 bash shell linux-disk-free
我正在使用“ df -h ”命令来获取我目录中的磁盘空间详细信息,它给了我如下响应:
现在我希望能够通过一些批处理或脚本自动执行此检查 - 所以我想知道,如果我能够只检查我关心的特定文件夹的磁盘空间,如图所示 - 我只应该检查/nas/home是否不超过 75%。
我怎样才能做到这一点?有什么帮助吗?
我的工作到现在:
我在用
df -h > DiskData.txt
Run Code Online (Sandbox Code Playgroud)
...这输出到一个文本文件
grep "/nas/home" "DiskData.txt"
Run Code Online (Sandbox Code Playgroud)
...这给了我输出:
*500G 254G 247G 51% /nas/home*
Run Code Online (Sandbox Code Playgroud)
现在,我希望能够搜索“%”符号之前或右侧附近的数字(在本例中为 51)以实现我想要的。
此命令将为您提供 /nas/home 目录的百分比
df /nas/home | awk '{ print $4 }' | tail -n 1| cut -d'%' -f1
Run Code Online (Sandbox Code Playgroud)
所以基本上你可以在某个变量中使用 store 作为值,然后应用 if else 条件。
var=`df /nas/home | awk '{ print $4 }' | tail -n 1| cut -d'%' -f1`
if(var>75){
#send email
}
Run Code Online (Sandbox Code Playgroud)