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 create -srcfolder packs a folder into a .dmg disk image

Terminal
~/tips/tip-macos-hdiutil-create-srcfolder-dmg

hdiutil create -volname "Project" -srcfolder ~/Documents/project project.dmg

Creates a compressed disk image from a folder, ready to share, mount or archive.

A disk image is a tidy way to hand over a folder: one file, mountable, with its own volume name. Disk Utility can make one, and so can a single command.

hdiutil create -volname "Project" -srcfolder ~/Documents/project project.dmg

-srcfolder copies the contents of the folder, file by file, into a new filesystem inside the image. -volname sets the name the volume gets when it mounts. According to the man page, a copy made this way ends up in UDZO format by default, a compressed read-only image, and the filesystem is APFS unless you pick another with -fs.

Opening and closing the image from the terminal uses two more verbs:

hdiutil attach project.dmg
hdiutil detach /dev/disk4

attach prints the device node and the mount point. Hand that device name, or the mount point, to detach afterwards. The disk number above is only an example, so use the one attach printed.

There is a safe check before you hand the file to anyone: hdiutil verify project.dmg compares the image against its stored checksum.

The caveat is the future. The current man page says hdiutil is deprecated as of macOS 27.0 and points to diskutil image for all disk image operations. It still works for what I describe here, but on a new macOS read the man page before you build a script around it.

Further reading: hdiutil man page

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