16

On macOS High Sierra, with SSD formatted with APFS, I've got:

   diskutil info -all

   Device Identifier:        disk0
   Device Node:              /dev/disk0
   Whole:                    Yes
   Part of Whole:            disk0
   Device / Media Name:      APPLE SSD SM0512L

   Volume Name:              Not applicable (no file system)
   Mounted:                  Not applicable (no file system)
   File System:              None

   Content (IOContent):      GUID_partition_scheme
   OS Can Be Installed:      No
   Media Type:               Generic
   Protocol:                 PCI-Express
   SMART Status:             Not Supported

   Disk Size:                500.3 GB (500277788672 Bytes) (exactly 977105056 512-Byte-Units)
   Device Block Size:        4096 Bytes

   Read-Only Media:          No
   Read-Only Volume:         Not applicable (no file system)

   Device Location:          Internal
   Removable Media:          Fixed

   Solid State:              Yes
   Hardware AES Support:     No

**********

   Device Identifier:        disk0s1
   Device Node:              /dev/disk0s1
   Whole:                    No
   Part of Whole:            disk0

   Volume Name:              Not applicable (no file system)
   Mounted:                  Not applicable (no file system)
   File System:              None

   Partition Type:           EFI
   OS Can Be Installed:      No
   Media Type:               Generic
   Protocol:                 PCI-Express
   SMART Status:             Not Supported
   Volume UUID:              E783267B-A4C3-3556-B751-DBED770EB996
   Disk / Partition UUID:    4C82BF11-A7F4-4477-A189-500D6D646FD2
   Partition Offset:         24576 Bytes (6 4096-Byte-Device-Blocks)

   Disk Size:                314.6 MB (314572800 Bytes) (exactly 614400 512-Byte-Units)
   Device Block Size:        4096 Bytes

   Read-Only Media:          No
   Read-Only Volume:         Not applicable (no file system)

   Device Location:          Internal
   Removable Media:          Fixed

   Solid State:              Yes
   Hardware AES Support:     No

**********

   Device Identifier:        disk0s2
   Device Node:              /dev/disk0s2
   Whole:                    No
   Part of Whole:            disk0

   Volume Name:              Not applicable (no file system)
   Mounted:                  Not applicable (no file system)
   File System:              None

   Partition Type:           Apple_APFS
   OS Can Be Installed:      No
   Media Type:               Generic
   Protocol:                 PCI-Express
   SMART Status:             Not Supported
   Disk / Partition UUID:    4A04D2CF-3476-402C-B7F7-891B43239CCA
   Partition Offset:         314597376 Bytes (76806 4096-Byte-Device-Blocks)

   Disk Size:                500.0 GB (499963170816 Bytes) (exactly 976490568 512-Byte-Units)
   Device Block Size:        4096 Bytes

   Read-Only Media:          No
   Read-Only Volume:         Not applicable (no file system)

   Device Location:          Internal
   Removable Media:          Fixed

   Solid State:              Yes
   Hardware AES Support:     No

**********

   Device Identifier:        disk1
   Device Node:              /dev/disk1
   Whole:                    Yes
   Part of Whole:            disk1
   Device / Media Name:      APPLE SSD SM0512L

   Volume Name:              Not applicable (no file system)
   Mounted:                  Not applicable (no file system)
   File System:              None

   Content (IOContent):      EF57347C-0000-11AA-AA11-00306543ECAC
   OS Can Be Installed:      No
   Media Type:               Generic
   Protocol:                 PCI-Express
   SMART Status:             Not Supported
   Disk / Partition UUID:    9A41D8CF-D5B1-4DD6-95B9-9AD553775115

   Disk Size:                500.0 GB (499963170816 Bytes) (exactly 976490568 512-Byte-Units)
   Device Block Size:        4096 Bytes

   Read-Only Media:          No
   Read-Only Volume:         Not applicable (no file system)

   Device Location:          Internal
   Removable Media:          Fixed

   Solid State:              Yes
   Virtual:                  Yes
   Hardware AES Support:     No

**********

   Device Identifier:        disk1s1
   Device Node:              /dev/disk1s1
   Whole:                    No
   Part of Whole:            disk1

   Volume Name:              Macintosh SSD
   Mounted:                  Yes
   Mount Point:              /

   Partition Type:           41504653-0000-11AA-AA11-00306543ECAC
   File System Personality:  APFS
   Type (Bundle):            apfs
   Name (User Visible):      APFS
   Owners:                   Enabled

   OS Can Be Installed:      Yes
   Booter Disk:              disk1s2
   Recovery Disk:            disk1s3
   Media Type:               Generic
   Protocol:                 PCI-Express
   SMART Status:             Not Supported
   Volume UUID:              63FC8459-50EA-467F-9808-FE1FDCA3202F
   Disk / Partition UUID:    63FC8459-50EA-467F-9808-FE1FDCA3202F

   Disk Size:                500.0 GB (499963170816 Bytes) (exactly 976490568 512-Byte-Units)
   Device Block Size:        4096 Bytes

   Volume Total Space:       500.0 GB (499963170816 Bytes) (exactly 976490568 512-Byte-Units)
   Volume Used Space:        311.0 GB (311046836224 Bytes) (exactly 607513352 512-Byte-Units) (62.2%)
   Volume Free Space:        188.9 GB (188916334592 Bytes) (exactly 368977216 512-Byte-Units) (37.8%)
   Allocation Block Size:    4096 Bytes

   Read-Only Media:          No
   Read-Only Volume:         No

   Device Location:          Internal
   Removable Media:          Fixed

   Solid State:              Yes
   Hardware AES Support:     No

**********

   Device Identifier:        disk1s2
   Device Node:              /dev/disk1s2
   Whole:                    No
   Part of Whole:            disk1

   Volume Name:              Preboot
   Mounted:                  No

   Partition Type:           41504653-0000-11AA-AA11-00306543ECAC
   File System Personality:  APFS
   Type (Bundle):            apfs
   Name (User Visible):      APFS
   Owners:                   Disabled

   OS Can Be Installed:      No
   Booter Disk:              disk1s2
   Recovery Disk:            disk1s3
   Media Type:               Generic
   Protocol:                 PCI-Express
   SMART Status:             Not Supported
   Volume UUID:              D781C0DD-86C5-45D3-AC8E-835B81757689
   Disk / Partition UUID:    D781C0DD-86C5-45D3-AC8E-835B81757689

   Disk Size:                500.0 GB (499963170816 Bytes) (exactly 976490568 512-Byte-Units)
   Device Block Size:        4096 Bytes

   Volume Total Space:       0 B (0 Bytes) (exactly 0 512-Byte-Units)
   Volume Free Space:        0 B (0 Bytes) (exactly 0 512-Byte-Units)

   Read-Only Media:          No
   Read-Only Volume:         Not applicable (not mounted)

   Device Location:          Internal
   Removable Media:          Fixed

   Solid State:              Yes
   Hardware AES Support:     No

**********

   Device Identifier:        disk1s3
   Device Node:              /dev/disk1s3
   Whole:                    No
   Part of Whole:            disk1

   Volume Name:              Recovery
   Mounted:                  No

   Partition Type:           41504653-0000-11AA-AA11-00306543ECAC
   File System Personality:  APFS
   Type (Bundle):            apfs
   Name (User Visible):      APFS
   Owners:                   Disabled

   OS Can Be Installed:      No
   Booter Disk:              disk1s2
   Recovery Disk:            disk1s3
   Media Type:               Generic
   Protocol:                 PCI-Express
   SMART Status:             Not Supported
   Volume UUID:              E223480E-7D24-4FBD-8936-60AFDD89865B
   Disk / Partition UUID:    E223480E-7D24-4FBD-8936-60AFDD89865B

   Disk Size:                500.0 GB (499963170816 Bytes) (exactly 976490568 512-Byte-Units)
   Device Block Size:        4096 Bytes

   Volume Total Space:       0 B (0 Bytes) (exactly 0 512-Byte-Units)
   Volume Free Space:        0 B (0 Bytes) (exactly 0 512-Byte-Units)

   Read-Only Media:          No
   Read-Only Volume:         Not applicable (not mounted)

   Device Location:          Internal
   Removable Media:          Fixed

   Solid State:              Yes
   Hardware AES Support:     No

**********

   Device Identifier:        disk1s4
   Device Node:              /dev/disk1s4
   Whole:                    No
   Part of Whole:            disk1

   Volume Name:              VM
   Mounted:                  Yes
   Mount Point:              /private/var/vm

   Partition Type:           41504653-0000-11AA-AA11-00306543ECAC
   File System Personality:  APFS
   Type (Bundle):            apfs
   Name (User Visible):      APFS
   Owners:                   Enabled

   OS Can Be Installed:      No
   Booter Disk:              disk1s2
   Recovery Disk:            disk1s3
   Media Type:               Generic
   Protocol:                 PCI-Express
   SMART Status:             Not Supported
   Volume UUID:              64D2BA51-6B59-4470-A71F-58E051AA54B5
   Disk / Partition UUID:    64D2BA51-6B59-4470-A71F-58E051AA54B5

   Disk Size:                500.0 GB (499963170816 Bytes) (exactly 976490568 512-Byte-Units)
   Device Block Size:        4096 Bytes

   Volume Total Space:       500.0 GB (499963170816 Bytes) (exactly 976490568 512-Byte-Units)
   Volume Used Space:        311.0 GB (311046836224 Bytes) (exactly 607513352 512-Byte-Units) (62.2%)
   Volume Free Space:        188.9 GB (188916334592 Bytes) (exactly 368977216 512-Byte-Units) (37.8%)
   Allocation Block Size:    4096 Bytes

   Read-Only Media:          No
   Read-Only Volume:         No

   Device Location:          Internal
   Removable Media:          Fixed

   Solid State:              Yes
   Hardware AES Support:     No

**********

I just have a 512 GB SSD.

So, my problem is how to correctly parse it to get:

  • Total Space
  • Available Space

From the output, I can't simply sum all the lines starting with:

Volume Total Space and Volume Free Space; it seems they both lists the same thing:

Volume Total Space: 500.0 GB (499963170816 Bytes) (exactly 976490568 512-Byte-Units)

Trying df -H, the output is:

df -H
Filesystem      Size   Used  Avail Capacity iused               ifree %iused  Mounted on
/dev/disk1s1    500G   309G   189G    63% 2397390 9223372036852378417    0%   /
devfs           195k   195k     0B   100%     658                   0  100%   /dev
/dev/disk1s4    500G   1.1G   189G     1%       3 9223372036854775804    0%   /private/var/vm
map -hosts        0B     0B     0B   100%       0                   0  100%   /net
map auto_home     0B     0B     0B   100%       0                   0  100%   /home

Again, there are 2 lines (for /dev/disk1s1 and /dev/disk1s4) showing:

Size 500G and Avail 189G

I know somehow this behavior is related to APFS thing; but again, I need some logic to filter out the duplicated entry .

I just want to use one or more CLI commands, which somehow let me respond this question:

How much space are on ALL disks (sum) on this host, and how much is available on ALL disks (sum) ?

1
  • What are you attempting to get? The free space of the drive? Why not use df?
    – Allan
    May 18, 2018 at 1:25

4 Answers 4

13

Both of the following solutions list the total disk size and the available space on the next line of a HFS filesystem:

df -H | awk '/\/dev\/disk1/ {printf("%s\n%s\n", $2,$4)}'

diskutil info /dev/disk1 | awk '
        /Volume Total Space/     { total = $4" "$5 }
        /Volume Available Space/ { avail = $4" "$5 }
        END { printf("%s\n%s\n", total, avail) }'

For the APFS filesystem try:

df -H | awk '/\/dev\/disk1s1/ {printf("%s\n%s\n", $2,$4)}'

diskutil info /dev/disk1s1 | awk '
        /Volume Total Space/     { total = $4" "$5 }
        /Volume Free Space/      { avail = $4" "$5 }
        END { printf("%s\n%s\n", total, avail) }'

EDIT: The capacity of all mounted filesystems and the total available space.

df | awk '/\/dev\/disk*/ && ! /\/private\/var\/vm/ {

        s_byte = $2 * 512            # covert blocks to bytes
        a_byte = $4 * 512
        ts_byte+=s_byte              # add bytes for each device
        ta_byte+=a_byte              # to their total
        }

        END {

        ts_byte=ts_byte/1000000000   # convert bytes to GB
        ta_byte=ta_byte/1000000000

        print "CAPACITY:", ts_byte,"G     AVAILABLE:", ta_byte,"G"
}'
1
  • 1
    Your solution is for a specific disk. I need the "global" values, the sum of all disks (total and available). May 19, 2018 at 12:00
5

You should use diskutil info /dev/disk1.


diskutil info -all gives more than needed also showing volumes which are used for restoring the system etc.

df is not useful here, cause it doesn't provide physical volume info.

4
  • This doesn't answer the OP's question.
    – Allan
    May 18, 2018 at 10:30
  • question was: how to correctly parse it to get: Total Space, Available Space. df is not useful here, cause it doesn't provide physical volume info. diskutil info --all gives more than needed also showing volumes which are used for restoring the system etc.. May 18, 2018 at 11:04
  • 1
    That's the info that should be added to the answer. Good answers will explain why they're good, or better than other answers, and why they answer the question. It would be great if you can edit your answer to add this info. See How to Answer for more info.
    – fsb
    May 18, 2018 at 13:00
  • tnx for the tip, fsb May 18, 2018 at 13:52
2

The answer from @fd0 was useful but didn't work for me on Big Sur - so here's an updated version of that script. It works for a single internal SSD, but would need changes for external disks to check each one separately.

Tested on Big Sur 11.4:

#!/bin/bash
set -euo pipefail

# Only use macOS versions of df, awk, etc
PATH=/bin:/usr/bin

# This works for a single internal disk that contains root FS 
# - tested on 11.4 Big Sur
df / | sed '1d' |
    awk '
        /^\/dev\/disk1s1s1/ {
            size_byte = $2 * 512            # df uses 512 byte blocks
            avail_byte = $4 * 512
            total_size_gb = size_byte / 1000000000
            total_avail_gb = avail_byte / 1000000000

            printf "Size: %.1f GB     Free: %.1f GB\n", total_size_gb, total_avail_gb
        }
    '

Example output:

$ disk-space-mac 
Size: 500.0 GB     Free: 424.8 GB

This command is useful to see the APFS volumes, all of which share the same free space on the containing /dev/disk1 virtual disk:

$ diskutil list virtual 
/dev/disk1 (synthesized):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      APFS Container Scheme -                      +500.0 GB   disk1
                                 Physical Store disk0s2
   1:                APFS Volume ⁨Macintosh HD⁩            15.3 GB    disk1s1
   2:              APFS Snapshot ⁨com.apple.os.update-...⁩ 15.3 GB    disk1s1s1
   3:                APFS Volume ⁨Macintosh HD - Data⁩     48.7 GB    disk1s2
   4:                APFS Volume ⁨Preboot⁩                 591.0 MB   disk1s3
   5:                APFS Volume ⁨Recovery⁩                622.9 MB   disk1s4
   6:                APFS Volume ⁨VM⁩                      9.7 GB     disk1s5
0

Another easier way to get your info is to select drive and right click, then select Get Info. A small window opens with various info. It will list capacity, available, and used numbers for the drive. I don't understand what they are using for the available number but you can take the capacity number and subtract the amount used from it. This should give you an accurate number for the amount of space available.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .