Bash:检测所有磁盘并显示可用空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13399882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:47:50  来源:igfitidea点击:

Bash: Detect all disks and show the free space

linuxmacosbashshellfreebsd

提问by Citizen SP

I'm running the following command to get the available space on my HHD. In this case I know the device (/dev/disk0s2).

我正在运行以下命令来获取我的 HHD 上的可用空间。在这种情况下,我知道设备 (/dev/disk0s2)。

I'm wondering how to first detect all the HDD's and then run the df command below to show the available disk space of all HDD's.

我想知道如何首先检测所有硬盘,然后运行下面的 df 命令来显示所有硬盘的可用磁盘空间。

$ df -h
Filesystem      Size   Used  Avail Capacity  Mounted on
/dev/disk0s2   112Gi   58Gi   54Gi    52%    /
/dev/disk0s3   800Gi   400Gi  400Gi   50%    /

 df -h | awk '=="/dev/disk0s2"{print }'

Result: 54Gi

结果:54Gi

Result I need: disk0s2: 54Gi, disk0s3: 400Gi, etc....

结果我需要:disk0s2:54Gi,disk0s3:400Gi,等等......

回答by choroba

Something like this?

像这样的东西?

df -h | tail -n+2 | while read fs size used rest ; do
    if [[ $used ]] ; then
        echo $fs $used
    fi
done