# 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](https://www.hexnode.com/mobile-device-management/help/how-to-run-scripts-on-mac-using-hexnode-mdm/) 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 
---------------

Script to list all disks on Mac

diskutil list 

   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 
---------------

Script to erase a disk on Mac

diskutil eraseVolume <Type> <New Name> <Identifier> 

   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
---------------

Script to format a volume on Mac

diskutil reformat <Identifier> 

   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 
----------------

Script to verify a volume on Mac

diskutil verifyVolume <Identifier> 

   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 
----------------

Script to repair a volume on Mac

diskutil repairVolume <Identifier> 

   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:

Script to unmount a disk volume

diskutil unmount <Identifier> 

   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: 

Script to mount a disk volume

diskutil mount <Identifier> 

   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`

 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.