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
List all disks
1 |
diskutil list |
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
1 |
diskutil eraseVolume <Type> <New Name> <Identifier> |
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
1 |
diskutil reformat <Identifier> |
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
1 |
diskutil verifyVolume <Identifier> |
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
1 |
diskutil repairVolume <Identifier> |
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:
1 |
diskutil unmount <Identifier> |
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:
1 |
diskutil mount <Identifier> |
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