Skip to content

Instantly share code, notes, and snippets.

@RayPS
Created July 11, 2025 05:31
Show Gist options
  • Save RayPS/58813a91128aa981d51b9a9c30cd7589 to your computer and use it in GitHub Desktop.
Save RayPS/58813a91128aa981d51b9a9c30cd7589 to your computer and use it in GitHub Desktop.
#!/bin/bash
if [[ $EUID -ne 0 ]]; then
echo "ERROR: Must be run with sudo" >&2
exit 1
fi
if [[ -z "$1" || ! -f "$1" ]]; then
echo "USAGE: $0 <disk_image_file>" >&2
exit 1
fi
IF=$1
disk_list=$(diskutil list external physical | grep 0:)
num_of_disks=$(echo "$disk_list" | wc -l)
echo "------------------------------------------------------------------------------"
echo " \#: TYPE NAME SIZE IDENTIFIER"
echo "$disk_list"
echo "------------------------------------------------------------------------------"
if [ $num_of_disks -eq 1 ]; then
disk_identifier=$(echo "$disk_list" | awk '{print $NF}')
else
if [ $num_of_disks -eq 0 ]; then
echo "External disk not found"
exit 1
else
read -p "Choose a Disk: /dev/disk" disk_number
disk_identifier="disk$disk_number"
fi
fi
OF="/dev/$disk_identifier"
echo "Unmounting $OF..."
diskutil unmountDisk $OF
echo "writing $IF to $OF"
pv $IF | dd of=$OF bs=4M
sync
echo "Finished writing, unmounting $OF..."
diskutil unmountDisk $OF
echo "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment