如何在Bash中获取当前的可用磁盘空间? [英] How to get the current free disk space in Bash?

查看:235
本文介绍了如何在Bash中获取当前的可用磁盘空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一些不断耗尽磁盘空间的操作。出于这个原因,我希望我的计算机在磁盘空间低于2GB时发出声音。我知道我可以通过运行 df -h 获得列出可用磁盘空间的输出:

I'm running some operations which constantly eat up my disk space. For this reason I want my computer to make a sound when disk space is running below 2GB. I know I can get an output listing the free diskspace by running df -h:

Filesystem                                      Size   Used  Avail Capacity   iused     ifree %iused  Mounted on
/dev/disk1                                     112Gi  100Gi   12Gi    90%  26291472   3038975   90%   /
devfs                                          191Ki  191Ki    0Bi   100%       663         0  100%   /dev
map -hosts                                       0Bi    0Bi    0Bi   100%         0         0  100%   /net
map auto_home                                    0Bi    0Bi    0Bi   100%         0         0  100%   /home

但我不能在if-then语句中使用此输出,以便在可用空间低于2GB时播放声音。

but I can't use this output in an if-then statement so that I can play a sound when the Available free space drops below 2GB.

有人知道如何才能获得可用空间而不是这个完整输出吗?

Does anybody know how I can get only the Available space instead of this full output?

推荐答案

首先,可用磁盘空间取决于您正在处理的分区/文件系统。以下命令将打印当前文件夹中的可用磁盘空间:

First, the available disk space depends on the partition/filesystem you are working on. The following command will print the available disk space in the current folder:

TARGET_PATH="."
df -h "$TARGET_PATH"  | awk 'NR==2{print $4}'

TARGET_PATH 是您要写入的文件夹。 df 自动检测文件夹所属的文件系统。

TARGET_PATH is the folder you are about writing to. df automatically detects the filesystem the folder belongs to.

这篇关于如何在Bash中获取当前的可用磁盘空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆