Currently part of my build process is as follows;

  • Start Utilities/Disk Utility
  • Select New Image
  • Set Save As to widget
  • Set Name to widget
  • Set Size to custom size 50 mb
  • Set Format Mac OS Extended

but is it possible to do all this from the command line ?


You can do this with the hdiutil tool. The appropriate configuration in your case would be:

hdiutil create -size 50m -fs HFS+ -volname Widget /path/to/save/widget.dmg

Obviously change the "Widget" and path to whatever you need.

A few additional options that may be useful:

  • -srcfolder /path/to/source This will create the disk image with the data in the specified folder as the contents.
  • -megabytes 50 used instead of -size 50m. This will use binary sized megabytes (2^20 bytes) instead of decimal (10^6 bytes).
  • -srcdevice /dev/diskXsY This is like srcfolder, but makes a block-based copy from another device, like a hard drive partition or DVD. Useful for making clones and images of install disks, etc.

I made a little bash script to automate a disc image creation.

It creates a temporary directory to store all needed files then export it in a new DMG file. Temporary directory is then deleted.

# Create .dmg file for macOS

# Adapt these variables to your needs

# The directory of the script
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# The temp directory used, within $DIR
WORK_DIR=`mktemp -d "${DIR}/tmp"`

# Check if tmp dir was created
if [[ ! "${WORK_DIR}" || ! -d "${WORK_DIR}" ]]; then
    echo "Could not create temp dir"
    exit 1

# Function to deletes the temp directory
function cleanup {
    rm -rf "${WORK_DIR}"
    #echo "Deleted temp working directory ${WORK_DIR}"

# Register the cleanup function to be called on the EXIT signal
trap cleanup EXIT

# Copy application on temp dir
cp -R "${APP_FILE}" "${WORK_DIR}"
# Copy other files without hidden files
rsync -a --exclude=".*" "${OTHER_FILES_TO_INCLUDE}" "${WORK_DIR}"

# Create .dmg
hdiutil create -volname "${DMG_NAME}" -srcfolder "${WORK_DIR}" -ov -format UDZO "${OUTPUT_DMG_DIR}/${DMG_NAME}.dmg"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .