Category filter

Script to execute Disk Utility operations on Mac

Disk Utility is a system application to manage internal and external storage devices on a Mac. Users can diagnose and repair hard drive issues with the Disk Utility app. It can perform erase, verify, and repair operations on storage volumes. Access the Disk Utility app on macOS devices from the Utilities folder under the Applications tab. To troubleshoot faulty device storage behavior on remote endpoints, admins can execute custom commands using the Execute Custom Script action. This document contains custom Bash commands for performing Disk Utility operations on remote devices.

Scripting Language – Bash

File extension – .sh

Disclaimer:


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

List all disks

The list command will list all disks and their partitions present on the device. The table will contain the disk type, disk name, storage size, and disk identifier information.

Erase a volume

For example:
diskutil eraseVolume APFS disk1s5

  • The eraseVolume command erases an existing volume and writes out a new empty file system.
  • Specify the file system format for the new volume in the <Type> field.
  • Provide the new name for the volume in the <New Name> field.
  • Replace <Identifier> with disk identifier.

Format a volume

For example:
diskutil reformat disk1s5

The reformat command erases the contents of the specified volume. The name and the file system format of the volume remain the same. Replace the <Identifier> field with the disk identifier.

Verify a volume

For example:
diskutil verifyVolume disk1s5

The verifyVolume command checks the file system data structures of a volume and checks if it needs repair.

Execute the repair command if the message “The volume Macintosh HD was found corrupt and needs to be repaired” is displayed during disk verification.

Repair a volume

For example: diskutil repairVolume disk1s5

Repair the file system data structures of a volume with the repairVolume command. The repair action will not be complete if other APFS volumes are mounted on the specified volume. Perform repair either by unmounting the other volumes or repair from Recovery System.

Performing the verify and repair command on the same volume is the command-line approach to the First Aid feature in Disk Utility.

Mount/Unmount a volume

To unmount a volume:

The unmount command ejects/unmounts the specified volume on the Mac. Replace the <Identifier> field with the disk identifier.

For example: diskutil unmount disk1s2

You can use this command to unmount any volume. On executing the command, the drive will ‘disappear’ from the Finder but will still be visible in the Disk Utility app on the Mac.

To mount a volume:

The mount command will mount/remount the specified volume on the Mac. Replace the <Identifier> field with the disk identifier.

For example: diskutil mount disk1s2

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