diskutil

On this page 7

diskutil is the macOS command-line utility for managing disks and volumes. It lists storage devices, creates and resizes partitions, formats and erases volumes, mounts and ejects media, verifies and repairs file systems, and manages APFS containers. It is the Terminal counterpart of the graphical Disk Utility app, and both rely on the same system framework.

In Terminal & scripting on macOS, diskutil matters because it works where the graphical app cannot: in scripts, remote SSH sessions and macOS Recovery, and with options the app does not expose. Many of its verbs are destructive and act immediately, so precise disk identifiers are essential.

How diskutil addresses disks and volumes

diskutil works with device identifiers such as disk0, disk3 or disk3s1, where the number after disk names a physical or synthesized disk and the s suffix names a slice or volume. The command diskutil list prints every disk with its partition map, sizes and identifiers, and it should be the first command in any disk session.

On modern Macs, an APFS container appears as a synthesized disk. The physical internal SSD might be disk0, while its APFS container appears as disk3 with volumes disk3s1, disk3s5 and others. Identifiers are not stable across restarts or reconnections, so scripts should resolve them at run time or use volume UUIDs.

The table below explains the main kinds of identifiers diskutil accepts.

IdentifierExampleStability
Device nodedisk4s2Changes between sessions
Mount point/Volumes/BackupChanges if the volume is renamed
Volume UUIDLong hexadecimal stringStable for the life of the volume
Disk path/dev/disk4Same as the device node
Identifier types accepted by diskutil

Core diskutil verbs

diskutil is organized around verbs that follow the command name, and APFS-specific operations sit under the apfs verb. Running diskutil with no arguments prints the full list, and adding a verb without parameters shows its usage.

VerbWhat it doesDestructive
listShows all disks, partitions and identifiersNo
infoPrints detailed properties of one disk or volumeNo
mount / unmountAttaches or detaches a volumeNo
ejectUnmounts all volumes and releases removable mediaNo
verifyVolumeChecks file system consistencyNo
repairVolumeRepairs a damaged file systemLow risk
eraseDiskErases a whole disk with a new partition mapYes
eraseVolumeReformats a single volumeYes
partitionDiskCreates a complete partition layoutYes
apfs addVolumeAdds a volume to an APFS containerNo
Frequently used diskutil verbs

A typical erase looks like diskutil eraseDisk APFS Archive GPT disk4: the file system format, the new volume name, the partition scheme and the target. The command diskutil listFilesystems shows which format names the current system accepts.

File systems and partition schemes in diskutil

diskutil can create several file system formats, and the right choice depends on which devices must read the disk. APFS is the default for Mac storage, while ExFAT is the common choice for drives shared with Windows.

Format nameBest forLimitation
APFSInternal SSDs, modern external drivesNot readable by Windows without extra software
JHFS+Older Macs and some Time Machine disksNo snapshots or space sharing
ExFATDrives shared between macOS and WindowsNo journaling, higher corruption risk
MS-DOS FAT32Small media, cameras, legacy devicesMaximum file size of 4 GB
Format choices and when to use them

The partition scheme sets how the disk is divided. GPT, the GUID Partition Table, is correct for almost every modern use, MBR remains for some older devices, and APM is a legacy Apple scheme for PowerPC-era Macs. A disk must be erased with GPT before macOS can install or boot from it.

Several macOS tools touch storage, and choosing the correct one avoids unnecessary risk. diskutil manages disks and volumes, whereas hdiutil works with disk image files such as .dmg and .sparsebundle, and tmutil controls Time Machine.

Disk Utility offers the same core functions with a safer interface, but it hides some options, such as non-default partition layouts and certain APFS commands. The low-level tools fsck_apfs and gpt give even finer control, yet they bypass the protections diskutil adds, so experienced administrators reserve them for recovery situations.

For automation, diskutil offers a machine-readable output mode. Adding -plist to verbs such as list and info returns a property list that scripts can parse with plutil or PlistBuddy, which is far more reliable than scraping the human-readable columns. Most modifying verbs also require administrator privileges through sudo when they touch internal disks or system volumes.

Safety rules and common mistakes

diskutil does not ask for confirmation before most destructive operations, so the main danger is targeting the wrong disk. A careful workflow always runs diskutil list immediately before an erase and confirms the size and name of the target.

MistakeCausePrevention
Erasing the wrong diskReused an old identifierRun diskutil list right before erasing
Unmount fails with resource busyAn app still holds open filesQuit the app or use lsof to find it
Repair reports errors on the boot volumeThe volume is in useRepair from macOS Recovery
Windows cannot read the driveFormatted as APFSUse ExFAT for cross-platform drives
Script breaks after a restartIdentifiers changedResolve by volume UUID
Mistakes, causes and fixes

On Apple silicon and T2 Macs, internal storage is always encrypted in hardware, so an erase that removes the encryption keys makes data practically unrecoverable. That is useful for secure disposal, and it also means a mistaken erase cannot be undone with recovery software.

Understanding it simply

Every Mac has a program called Disk Utility for preparing and fixing drives. diskutil is the same toolbox operated by typing commands instead of clicking buttons.

Typing commands is faster when the same job must be repeated or done on a remote computer. It is also less forgiving: diskutil does exactly what it is told, with no second question. That is why careful users always check which drive they are pointing at before erasing anything.

Applied in practice

An IT administrator at a design studio prepares twenty external SSDs for project archiving. Clicking through Disk Utility twenty times would take a long time and invite mistakes, so she writes a short zsh script around diskutil.

For each drive, the script runs diskutil list external physical, checks that the reported size is about 2 TB, and only then runs diskutil eraseDisk APFS Archive GPT with that identifier. A drive with an unexpected size is skipped and logged rather than erased.

The whole batch finishes in under fifteen minutes. One drive is flagged because a colleague had connected a personal disk of a different size, and the size check prevented data loss. Each archive drive is formatted identically, which simplifies later backup verification.