diskutil in macOS

CLI powerhouse for inspecting & managing physical / virtual disks

diskutil is Apple’s Swiss-army knife for storage: listing, partitioning, reparing, erasing, APFS & RAID administration, CoreStorage control, image mounting, scripting and much more

Basic usage pattern:

diskutil verb [options] [targets…]

  1. Run diskutil list to discover the correct /dev/diskX identifier.
  2. Apply other verbs to that identifier (e.g., info, eraseDisk, etc.).

Inspecting Disks

Enumerate & query metadata safely before touching data

1 Discover devices

diskutil list Shows the system’s BSD names, containers, partitions, and file-systems

2 Detailed information

diskutil info /dev/disk2s1  or  diskutil info -all

Tip: pipe through grep or jq when using -plist for scripts.

Partitioning & Formatting

Create, resize or wipe volumes (HFS+, APFS, ExFAT…)

1 Erase whole device

diskutil eraseDisk APFS "My SSD" /dev/disk4

2 Non-destructive partition add/remove

diskutil partitionDisk /dev/disk2 3 GPT APFS "Data" 100G MS-DOS "BOOT" 16G Free Space 0B

3 Secure erase levels (rotational media only)

LevelPasses / Pattern
0Single-pass zeros (quick)
1Single-pass random
2US-DoD 3-pass
3US-DoD 7-pass
435-pass Gutmann

diskutil secureErase 1 /dev/disk3

On SSDs, rely on FileVault + whole-disk erase; repeated overwrites are ineffective

Managing APFS

Containers, volumes & snapshots in Apple’s modern FS

1 Inspect

diskutil apfs list

2 Create container

diskutil apfs createContainer /dev/disk2s2

3 Add volume

diskutil apfs addVolume disk2 APFS "Projects"

4 Resize (live)

diskutil apfs resizeContainer disk2 0  ⟶ expand to fill device.

5 Snapshots

diskutil apfs listsnapshots /

diskutil apfs deleteSnapshot disk2s5 -uuid UUID-HERE

Mounting & Ejecting

Bring volumes online or take them safely offline

diskutil mountDisk /dev/disk3 — mounts every mount-able volume on the physical.

diskutil mount -readonly /dev/disk3s1

diskutil unmountDisk /dev/disk3

Always unmount before physical removal or imaging.

Verification & Repair

First-aid for file-system corruption

diskutil verifyDisk /dev/disk1  – checks partition map.

diskutil verifyVolume /dev/disk1s2

diskutil repairVolume /Volumes/Macintosh HD

On APFS, these call fsck_apfs in live-verify mode

AppleRAID & CoreStorage

Legacy tools still ship with diskutil

Create stripe:

diskutil appleRAID create stripe "Scratch RAID0" JHFS+ disk3 disk4

Convert CoreStorage into APFS:

diskutil coreStorage convert LVUUID

AppleRAID/CoreStorage are deprecated—prefer APFS volumes or H/W RAID.

Automation & Scripting

Use -plist, JSON & exit status for robust scripts

  1. diskutil info -plist disk2s1 | plutil -convert json -o - - -
  2. Check $? to branch on success / failure.
  3. Wrap dangerous verbs with bash -euo pipefail.

Apple Configurator & MDM workflows may call diskutil under the hood.

Cheat-Sheet Recipes

Quick copy-paste for everyday ops

GoalCommand
Clone bootable USB sudo asr --source /dev/disk4 --target /dev/disk5 --erase
Enable journaling (HFS+) diskutil enableJournal /Volumes/ARCHIVE
Rename volume diskutil rename /Volumes/OLD NewName
Eject all externals diskutil external | xargs -I{} diskutil unmountDisk {}

Best Practices & Caveats

Avoid data loss; maximise success rates

  1. Always begin with diskutil list; confirm target identifiers twice.
  2. Unmount before hot-unplugging to flush write-buffers.
  3. Use APFS snapshots for “instant” rollbacks rather than cloning whole drives.
  4. Prefer FileVault to “secure erase” on SSDs—over-provisioned blocks remain inaccessible.
  5. Run Time Machine / backup before any erase or repair verbs.

References

Man-pages & Apple technical notes

  1. macOS diskutil man page via SS64
  2. iBoysoft Diskutil guide
  3. Apple Support: Secure erase & Disk Utility UI articles
  4. EasyOSX secureErase tutorial
  5. APFS command-line tools discussion