Creating a Mac DMG file from a folder/directory

It’s a tiny thing you never really think about until you need to do it.  How do you put a folder/directory with it’s files into a single DMG file so you can double-click on it to mount it?  Well, I had to figure it out for the first time and while it’s simple once you know it, it’s nicer when someone just points it out.  It can be done through Disk Utility by clicking on the “New Image” icon on the top and filling out the parameters.  That’s the simple way to do it, but for those of you like myself who would rather have a commandline way of doing things, welcome to my world.

The hdiutil command gives the ability in one foul swoop.  The following will create a dmg file in my home directory named test.dmg, and copy all of the contents of a folder named “TestFolder” in my home directory directly to it.  This uses the default Apple file system HFS+, but there are other selections that can be seen in the man page for it by typing ‘man hdutil’.
hdiutil create -fs HFS+ -srcfolder /Users/danlund/TestFolder/ -volname TestName /Users/danlund/test.dmg

I recommend reading the man page on hdiutil, as there is a world of options that involve encryption, hybrid filesystem styles to mount on windows and os x, along with creating a virtual disk that can be read/write.  Pretty nifty if you need to isolate files and keep them encrypted separate from other files on the same disk.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.