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
Create a disk image from a folder
Execute the following script to create a disk image from a folder on the device.
1 2 |
#!/bin/sh hdiutil create -fs HFS+ -srcfolder /path/to/folder -volname “name” /path/to/diskimage/name.dmg |
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.
1 2 |
#!bin/sh hdiutil create -fs HFS+ -srcfolder /Users/Shared -volname “Main” /Users/username/Documents/Main.dmg |
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.