Category filter

Script to create a disk image from a folder on macOS devices

A disk image is a file that holds a replica of the data on a disk volume or storage device. As an administrator, you may need to create a disk image from a folder to be able to mount it on other devices/servers conveniently. Disk image provides you with extended control over the availability and accessibility of this folder and its contents. For instance, you can put up one disk image on multiple machines and quickly set up several computers in one go instead of setting them up separately. By creating a disk image of a computer drive, you can also store the data from a computer without keeping the actual hardware around. Additionally, disk images can help you transfer large files or programs over the web. With Hexnode’s Execute Custom Script action, you can remotely create a disk image from a folder on your macOS devices using this bash script.

Scripting Language – Bash

File extension – .sh

Disclaimer:


The Sample Scripts provided below are adapted from third-party Open-Source sites.

Create a disk image from a folder

Execute the following script to create a disk image from a folder on the device.

For example, the given script creates a disk image named ‘Main’ in the path /Users/username/Documents with the contents of the folder /Users/Shared.

hdiuitil – A command-line utility on Mac to manipulate disk images.

create – Used to create a disk image.

-fs filesystem – Decides the type of filesystem to be written to the image. Available options are HFS+, HFS+J (JHFS+), HFSX, JHFS+X, MS-DOS, or UDF.

-srcfolder source – Creates a new filesystem on the specified path by copying contents of the source into the disk image.

-volname volname – Used to specify a name for the newly created disk image.

Notes:

  • It is recommended to manually validate the script execution on a system before executing the action in bulk.
  • Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.

  • Sample Script Repository