Overview

What diskutil is and when to use it

diskutil is Apple’s command-line Swiss-army knife for managing disks, partitions, APFS containers, Core Storage groups, and external media on macOS. It exposes far more verbs (“sub-commands”) than the Disk Utility GUI, ranging from simple listings to live container resizing.

Syntax

$ diskutil [quiet] verb [options]

Why use the CLI?

  1. Automate repetitive disk tasks in scripts or MDM workflows.
  2. Access functionality the GUI hides (e.g., apfs addVolume, live partition edits).
  3. Troubleshoot when the graphical Disk Utility fails to launch or stalls.

Inventory & Discovery

Listing devices, volumes, and containers

Essential verbs

VerbDescription
listShow all disks, partitions, APFS objects.
info deviceDetailed properties for a single disk/partition.
activityLive event stream of mounts, ejects, renames.
apfs listTree view of APFS containers & volumes.
cs listLegacy Core Storage logical volume groups.

Quick examples

# All media
$ diskutil list

# Inspect the boot volume
$ diskutil info /         

# APFS hierarchy only
$ diskutil apfs list

Formatting & Erasure

Safely wiping or re-formatting disks & volumes

Whole-disk erase

$ diskutil eraseDisk APFS "Macintosh HD" GPT /dev/disk3

Specify Scheme (MBR, GPT or APM), Filesystem (JHFS+, APFS, ExFAT…), a volume label, and the physical device node.

Single-volume erase

$ diskutil eraseVolume JHFS+ Data /Volumes/USB

Secure wipes

On rotational disks use secureErase (N-pass overwrite). On SSD/APFS the command zeros the metadata; rely on FileVault for cryptographic erasure instead.

Partition Management

Creating, resizing, deleting partitions live

Add a partition

$ diskutil addPartition disk0s2 MS-DOS BOOT 20G

Re-partition an empty drive in one shot

$ diskutil partitionDisk /dev/disk2 GPT \
  APFS System 250G \
  MS-DOS SHARE 100G \
  0B

Shrink or grow APFS container

$ diskutil apfs resizeContainer disk1s2 400G

APFS Workflows

Modern container & snapshot operations

Create container + volume in one step

$ diskutil apfs create /dev/disk3 "Data"

Add additional volumes with quotas

$ diskutil apfs addVolume disk4 APFS Projects \
  -reserve 50g -quota 120g

Snapshots

# List snapshots
$ diskutil apfs listSnapshots /        

# Create snapshot via filesystem utility first; then:
$ tmutil snapshot          # or fs_snapshot_create()

Repair & Verification

First Aid without the GUI

Verify & repair filesystems

$ diskutil verifyVolume /Volumes/Backup
$ diskutil repairVolume /dev/disk4s1

Repair partition maps (advanced)

$ diskutil repairDisk /dev/disk3

Mounting & Ejecting

Attach, detach, and force operations

Common actions

$ diskutil mount /dev/disk3s1
$ diskutil unmount /Volumes/USB
$ diskutil eject disk3

Force unmount (use sparingly)

$ diskutil unmountDisk force /dev/disk3

Scripting Tips

Automating diskutil in Bash or Python

Quiet mode

Add the quiet flag to suppress chatty progress lines—ideal for LaunchAgents.

Machine-readable output

$ diskutil info -plist disk0 | plutil -convert json - - o -

Detecting new media

Use diskutil activity in a background process to parse attach/detach events and trigger follow-up actions.

Safety Checklist

Avoiding catastrophic data loss

  1. Always confirm the /dev/diskN identifier with list before erasing.
  2. Keep verified backups; diskutil operations are often irreversible.
  3. Prefer live-resize (apfs resizeContainer) over manual repartitioning when possible.
  4. On SSDs use FileVault encryption plus eraseVolume for “instant” crypto-erase.

References & Further Reading

Official manuals & community guides