brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: hdiutil compact gives back the space a sparse bundle no longer uses

Terminal
~/tips/tip-macos-hdiutil-compact-sparsebundle

hdiutil compact ~/Documents/Projects.sparsebundle

Shrinks a sparse bundle disk image after you deleted files inside it.

A sparse bundle is my favourite kind of disk image for files that keep changing: it starts small and grows as you add data. The catch is that it doesn’t shrink on its own when you delete things inside it. That’s what hdiutil compact is for.

A sparse bundle (UDSB) stores its data in band files of 8 MB each, inside a bundle folder that Finder shows as one item. When you write data, new bands appear. When you delete files inside the mounted image, the bands stay. hdiutil compact scans the bands, and for a sparse bundle it simply removes the ones that are completely unused:

hdiutil compact ~/Documents/Projects.sparsebundle

An example: you keep old project files in a sparse bundle, delete a 20 GB video export from it, and the bundle on your disk is still just as big. After ejecting it and running the command above, the unused bands are gone. Encrypted images work too; hdiutil asks for the password. You can make a sparse bundle with Disk Utility (File > New Image > Blank Image, Image Format Sparse Bundle Image) or with hdiutil create -type SPARSEBUNDLE.

A few caveats. The man page says compact “may or may not” shrink the image, depending on where the files sit inside it, so don’t expect it to hit the exact size of the data. I’d run it on an image that isn’t mounted. And it won’t run on battery unless you add -batteryallowed; the man page warns that a power loss during a compact can damage a SPARSE image, so I’d plug in the charger rather than use that flag. Closing the lid cancels the job as well. Apple also mentions that sparse bundles are often used for Time Machine backups over a network; I’d leave those alone and let Time Machine manage them.

Further reading: hdiutil man page (SS64 mirror of Apple's man page), Create a disk image using Disk Utility

More Terminal tips ioreg -rn AppleSmartBattery | grep CycleCount Terminal can save your windows and tabs as a window group Export a Terminal profile to a .terminal file and import it on another Mac mc hidutil property --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000039,"HIDKeyboardModifierMappingDst":0x700000029}]}' All Terminal tips (116) All topics

Link