Mac OS X “.dmg” File and Command Line Tutorial
This document explains Mac OS X's “.dmg” file, and some tips about using command line tools for Mac specific things, such as launching apps, put system to sleep, etc.
What's “.dmg” File?
What is a disk image? Why not just zip?
Disk image files are different than compressed files. Disk image is roughly a copy of the actual structure of disk. The OS will treat disk images just like a storage device, but a virtual one. For example, you can create a zip version of all files on a CD, but that zip file can't be used by the OS to boot the machine. With disk image file, it can. With disk image files, you can also create a image file of a CD, and that can be used to create a CD that is identical to the original CD.
Mac OS X's disk image file format is called “.dmg” files. For applications or executable files, it's better to use disk image format, because that way, all resource fork issues are done correctly, among other reasons.
How to create .dmg files using command line?
To create one, use hdiutil, like this:
hdiutil create -megabytes 5 -fs HFS+ -volname myDisk ~/Desktop/myDisk
This will create a disk image with 5 mega bytes, using HFS+ format (OS X standard), and the name of the disk is “myDisk”, and the disk image file created is “myDisk.dmg”.
How to open a “.dmg” file with commnand line tool?
You can just use the “open” command. Like this:
The disk image will show up at
You can also use “hdid”, like this:
If it is encrypted, a password GUI dialog will pop up.
To attach a encrypted dmg file without having it pop up a GUI dialog, do
hdid -stdinpass mydisk.dmg.
How to eject a “.dmg” file using command line?
To eject, first find out what disk device it is associated to by typing “df”. Here's a sample output:
/dev/disk1s2 31420 27428 3992 88% /Volumes/financial
then, use hdiutil to eject it, for example:
hdiutil detach /dev/disk2s2.