MacOS – External disk reports incorrect amount of free space after doing clean up via terminal. DiskUtility’s Repair temporarily fixes it, but why

disk-utilityfinderhard drivehfs+macos

I have an external 5 TB USB drive (HFS extended) which contains one encrypted disk image (HFS extended) of an equal size (practically 5 TB). All the external disk's work files are inside the encrypted disk image.

Over time the available free space naturally shrunk and shrunk; and recently I did a bit of a clean up.

Some folders had accumulated ridiculous amounts of files so I decided to use terminal (instead of Finder) for the task, ie. files were removed with rm.

Moreover, I noticed some backup folders had (partially) duplicated content. I knew the filenames were unique, so I created a catch-all folder and moved all suspected duplicates to it (namely, with mv via terminal—probably not the best option in hindsight) effectively killing the duplicate files.

Now I'm sure I have plenty of free space available, but Finder doesn't seem to know it nor remember it.

After every remount, Finder, Disk Utility app and df report the space usage as follows (disk2s2 is the regular volume, and disk3s2 is the encrypted one):

$ df -h
Filesystem                                 Size   Used  Avail Capacity   iused   ifree %iused  Mounted on
/dev/disk2s2                              4.5Ti  4.5Ti  317Mi   100% 610375939   40592  100%   /Volumes/Snake
/dev/disk3s2                              4.5Ti  4.5Ti  285Mi   100% 610178600   36496  100%   /Volumes/87

Which is to say I have 285 MB free space ≈pretty much what I had before clean up.

Now—if I run "Repair" in Apple's Disk Utility app (on the encrypted disk image, ie. disk3s2), I get no errors, but free space is updated (and presumably corrected):

$ df -h
Filesystem                                 Size   Used  Avail Capacity   iused    ifree %iused  Mounted on
/dev/disk2s2                              4.5Ti  4.5Ti  317Mi   100% 610375920    40611  100%   /Volumes/Snake
/dev/disk3s2                              4.5Ti  4.2Ti  316Gi    94% 568746945 41468151   93%   /Volumes/87

ie. 316 GB available and no effects on disk2s2. I unmounted disk3s2 and scanned disk2s2, but neither that had any effect:

$ df -h
Filesystem                                 Size   Used  Avail Capacity   iused   ifree %iused  Mounted on
/dev/disk2s2                              4.5Ti  4.5Ti  317Mi   100% 610375930   40601  100%   /Volumes/Snake

Also the corrected amount of free space is lost yet again after remounting disk3s2:

$ df -h
Filesystem                                 Size   Used  Avail Capacity   iused   ifree %iused  Mounted on
/dev/disk2s2                              4.5Ti  4.5Ti  317Mi   100% 610375931   40600  100%   /Volumes/Snake
/dev/disk3s2                              4.5Ti  4.5Ti  285Mi   100% 610178592   36504  100%   /Volumes/87

Rebooting OS X does not help.

I tried secure erasing the free space by sudo diskutil secureErase freespace 0 /dev/disk3s2, but that didn't help at all.

  1. What magic does Disk Utility's Repair do that fixes the reported free space?
  2. Can I do just that manually? By that I mean, I don't need to run diagnostic checks, only the thing to get the space reporting right
  3. Why the repaired space usage report does not persist‽

Best Answer

To back up a little for others, in the future.

How to get a report of remaining storage.

Via Terminal, type df -h and you’ll get a human-readable summary by volume in columns marked Used, Avail, and Capacity (as a percentage). (The Gi or Ti refers to gibibytes and tebibytes, base 2 units for a billion and trillion bytes instead of the base 10 gigabytes and terabytes.)

enter image description here

If these don’t agree, it’s likely a Spotlight error. Apple relies on Spotlight to mark and calculate remaining storage. You can delete the Spotlight index and rebuild it by following these steps:

  1. Open the Spotlight preference pane.
  2. Click the Privacy tab.
  3. Drag the startup volume into the Privacy window.
  4. Click OK at the warning, and the Spotlight index is deleted.
  5. Select your startup volume in the privacy list, and click the - (minus) button.

That restarts indexing. This should reset your storage.