What is a DMG file? How does Linux create a DMG file from the command line


In Linux system operation, many people don’t know much about DMG files, let alone how to create DMG files. This article introduces the method of using the command line to create DMG files in Linux. Friends who don’t know can learn about it.


What is a DMG file and how to open it?

DMG format is a mirror file or compressed file on the MAC system. If you use a PC and do not install Mac OS X for x86, you don’t have to work hard. If you use a Mac or have Mac OS X for x86 installed on your PC, double-click this file on the MAC system to unlock it; If you want to burn this file into a DVD, use toast to burn the file into a CD: DMG = “disc image”

Create DMG from the command line

Automatically obtain the software version number


  VERSION=$(/usr/libexec/plistbuddy -c Print:CFBundleShortVersionString: “${APP_NAME}.app/Contents/Info.plist”)






Create dmg

#Clean up folder

  rm -rf “${STAGING_DIR}” “${DMG_TMP}” “${DMG_FINAL}”

#Create folder, copy, calculate

  mkdir -p “${STAGING_DIR}”

  cp -rpf “${APP_NAME}.app” “${STAGING_DIR}”

  SIZE=`du -sh “${STAGING_DIR}” | sed ‘s/([0-9.]*)M(.*)/1/’`

  SIZE=`echo “${SIZE} + 1.0” | bc | awk ‘{print int($1+0.5)}’`

#Fault tolerant processing

  if [ $? -ne 0 ]; then

  echo “Error: Cannot compute size of staging dir”



#Create a temporary DMG file

  hdiutil create -srcfolder “${STAGING_DIR}” -volname “${VOL_NAME}” -fs HFS+

  -fsargs “-c c=64,a=16,e=16” -format UDRW -size ${SIZE}M “${DMG_TMP}”

  echo “Created DMG: ${DMG_TMP}”

Set dmg

  DEVICE=$(hdiutil attach -readwrite -noverify “${DMG_TMP}” |

  egrep ‘^/dev/’ | sed 1q | awk ‘{print $1}’)

  sleep 2 

#Add a soft link to the applications directory

  echo “Add link to /Applications”

  pushd /Volumes/“${VOL_NAME}”

  ln -s /Applications


#Copy background picture

  mkdir /Volumes/“${VOL_NAME}”/.background

  cp “${DMG_BACKGROUND_IMG}” /Volumes/“${VOL_NAME}”/.background/

#Use Applescript to set a series of window properties

  echo ‘

  tell application “Finder”

  tell disk “’${VOL_NAME}‘”


  set current view of container window to icon view

  set toolbar visible of container window to false

  set statusbar visible of container window to false

  set the bounds of container window to {400, 100, 938, 432}

  set viewOptions to the icon view options of container window

  set arrangement of viewOptions to not arranged

  set icon size of viewOptions to 72

  set background picture of viewOptions to file “.background:’${DMG_BACKGROUND_IMG}‘”

  set position of item “’${APP_NAME}‘.app” of container window to {160, 195}

  set position of item “Applications” of container window to {360, 195}



  update without registering applications

  delay 2

  end tell

  end tell

  ’ | osascript



  hdiutil detach “${DEVICE}”

Compressed dmg

  echo “Creating compressed image”

  hdiutil convert “${DMG_TMP}” -format UDZO -imagekey zlib-level=9 -o “${DMG_FINAL}”

#Clean up folder

  rm -rf “${DMG_TMP}”

  rm -rf “${STAGING_DIR}”

  echo ‘Done.’


The above is an introduction to how Linux uses the command line to create DMG files. The above describes in detail what DMG files are, so that users can have a deeper understanding of DMG files.

Recommended Today

The whole tutorial of docker installation and use, installation / complete command / dockerfile image production / docker container arrangement and one click installation of nginx + redis + MySQL / visualizer portal (version 2022)

官网: https://docs.docker.com/ 官网: https://www.docker.com/ docker 镜像市场: https://hub.docker.com/ 一、docker 说明 1.1、docker 核心 1、Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从 Apache2.0 协议开源,Docker 是一个 CS 架构软件。 2、Docker 是一个虚拟化轻量级linux服务器,可以解决我们在开发环境中运行配置问题 3.、Docker的主要目标是‘build ,ship and run any app,anywhere’,一次封装,到处运行 4、容器是完全使用沙箱机制,相互之间不会有任何接口(类似 iPhone 的 app),更重要的是容器性能开销极低。 1.2、docker 版本问题 .Docker 从 17.03 版本之后分为 CE(Community Edition: 社区版) 和 EE(Enterprise Edition: 企业版),我们用社区版就可以了。 1.3、docker 架构( 3大核心) · 1、Images 镜像 (等于软件) · 2、Registry […]