macOS·29 questions

How to use the hdiutil terminal utility to create, compress, and encrypt disk images in macOS?

Answer

The hdiutil command-line utility is a powerful built-in macOS tool for working with DMG, ISO, and CDR disk images directly from the terminal without using third-party graphical software.

To create a new blank disk image of a specific size, you can use the container creation command. Open Terminal and run the command hdiutil create -size 500m -fs HFS+ -volname "MyFolder" myimage.dmg, which will create a five-hundred-megabyte disk image with the Mac OS Extended file system and the MyFolder volume label.

If you need to protect sensitive data inside the created image with a password, the utility allows you to apply encryption algorithms. Add the encryption flag to the creation command as follows: hdiutil create -size 1g -encryption AES-256 -volname "SecureDisk" secured.dmg. The system will ask you to enter a strong password twice, which will be required every time you mount this image.

To turn an existing folder with files into a ready-to-use DMG image, the conversion command is used. Enter hdiutil create -volname "Archive" -srcfolder /path/to/folder -ov final_archive.dmg, specifying the real path to your folder, and the utility will pack all nested files into a single disk image file.

In cases where the disk image takes up too much space and needs to be prepared for network transfer, the compression feature is applied. Run the command hdiutil convert input.dmg -format UDRO -o compressed.dmg, and the system will recreate the file in a format with the maximum compression degree, reducing its final size on disk.

Was this answer helpful?

More questions in this topic

Related questions from other topics