检查bash中当前分区的可用磁盘空间 [英] Check free disk space for current partition in bash

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

问题描述

我正在用bash编写安装程序.用户将转到目标目录并运行安装脚本,因此第一个操作应该是检查是否有足够的空间.我知道df将报告所有文件系统,但是我想知道是否有一种方法可以仅针对目标目录所在的分区获取可用空间.

I am writing an installer in bash. The user will go to the target directory and runs the install script, so the first action should be to check that there is enough space. I know that df will report all file systems, but I was wondering if there was a way to get the free space just for the partition that the target directory is on.

编辑-我想出的答案

df $PWD | awk '/[0-9]%/{print $(NF-2)}'

有点奇怪,因为df似乎会格式化其输出以适合终端,因此对于长安装点名称,输出会向下移动一行

Slightly odd because df seems to format its output to fit the terminal, so with a long mount point name the output is shifted down a line

推荐答案

是:

df -k .

当前目录.

df -k /some/dir

如果要检查特定目录.

您可能还想签出stat(1)命令(如果您的系统具有该命令).您可以指定输出格式,以使脚本更易于解析.这是一个小例子:

You might also want to check out the stat(1) command if your system has it. You can specify output formats to make it easier for your script to parse. Here's a little example:

$ echo $(($(stat -f --format="%a*%S" .)))

这篇关于检查bash中当前分区的可用磁盘空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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