Introduction
Managing disks in macOS through the graphical interface (Disk Utility) is convenient for most tasks. However, the terminal offers far greater control, especially for automation, remote work, or when the GUI is unavailable. In this guide, you will learn how to use built-in command-line utilities for disk operations: from simple free space checks to formatting and partitioning.
All commands work on modern versions of macOS (Monterey, Ventura, Sonoma). Attention: some commands, especially diskutil eraseDisk, permanently delete data. Always verify the disk identifier before performing dangerous operations.
Essential Commands for Disk Management
macOS provides several key utilities. Here are the most important ones.
diskutil: The Swiss Army knife
diskutil is the primary utility for managing disks and partitions. It replaces many functions of Disk Utility.
Key subcommands:
diskutil list— show all disks and partitions.diskutil info /dev/diskX— detailed information about a disk.diskutil mount /dev/diskXsY— mount a partition.diskutil unmount /dev/diskXsY— unmount a partition.diskutil eraseDisk Format Name /dev/diskX— format an entire disk.diskutil partitionDisk /dev/diskX Format Name ...— create partitions.
df and du: Checking Space Usage
df(disk free) shows free and used space on mounted filesystems.df -h— in human-readable format (GB, MB).df -i— shows inode usage (useful for "No space left on device" errors when space is available).
du(disk usage) estimates space usage by files and directories.du -sh /path/to/folder— total folder size in readable format.du -h --max-depth=1 /— size of each folder in the root (requiressudofor some system folders).
mount and umount: Mount Management
mount— show all mounted filesystems (similar todf).umount /dev/diskXsY— unmount a partition (legacy syntax;diskutil unmountis preferred).
Other Useful Commands
ls /Volumes— view mounted volumes (external disks) as folders.diskutil verifyDisk /dev/diskX— verify disk for errors (equivalent to "First Aid").sudo fdisk -l— list all partitions (requires password; shows non-APFS partitions too).
Practical Examples
Viewing All Disks and Partitions
diskutil list
Output will look similar to:
/dev/disk0 (internal, physical):
#: TYPE NAME SIZE IDENTIFIER
0: GUID_partition_scheme *512.1 GB disk0
1: EFEFI EFI 314.6 MB disk0s1
2: Apple_APFS Container disk1 511.8 GB disk0s2
/dev/disk1 (synthesized):
#: TYPE NAME SIZE IDENTIFIER
0: APFS Container Scheme - +511.8 GB disk1
Physical Store disk0s2
1: APFS Volume Macintosh HD 465.3 GB disk1s1
2: APFS Volume Preboot 52.2 MB disk1s2
3: APFS Volume Recovery 528.7 MB disk1s3
4: APFS Volume VM 4.2 GB disk1s4
Note the identifiers: disk0 is the physical disk, disk1 is the APFS container, disk1s1 is a volume inside the container.
Checking Free Space on the Main Disk
df -h /
Example output:
Filesystem Size Used Avail Capacity iused ifree %iused Mounted on
/dev/disk1s1 466G 420G 46G 91% 1000000 1000000000 0% /
Here you can see that on the main partition (Macintosh HD), 46 GB out of 466 GB is free.
Formatting an External Disk
Warning: all data on the disk will be erased!
- Connect the external disk.
- Find its identifier:
diskutil list. Let's say it'sdisk2. - Perform the format (e.g., in exFAT for Windows and macOS compatibility):
diskutil eraseDisk exFAT MyExternalDisk /dev/disk2
After completion, the disk will appear as MyExternalDisk in /Volumes.
Creating a New Partition on a Disk
If you have free space on a disk (e.g., disk2), you can create a new partition:
diskutil partitionDisk /dev/disk2 GPT JHFS+ NewPartition 100g
This command:
- Uses the GPT scheme.
- Creates one partition in JHFS+ (macOS Extended) format, 100 GB in size, named "NewPartition".
- Leaves the remaining space unallocated (you can create more partitions).
Mounting and Unmounting Disks
If a disk doesn't mount automatically:
diskutil mount /dev/disk2s1
For safe ejection (unmounting) before disconnecting:
diskutil unmount /dev/disk2s1
⚠️ Important: never disconnect an external disk without unmounting it via
diskutil unmountor through Finder (right-click → "Eject"). This can lead to data loss.
Important Warnings
- Always verify the disk identifier (
diskutil list) before anyeraseorpartitionoperation. A mistake in the identifier (e.g.,disk0instead ofdisk2) can result in complete data loss on the system disk. - When formatting to APFS, use
APFS(orAPFS (Encrypted)for encryption). For compatibility with older macOS versions, useJHFS+. - Commands requiring superuser privileges (e.g.,
diskutil verifyDiskon the system disk) will ask for a password. Enter your administrator password. df -hshows only mounted filesystems. If a disk is connected but not mounted, it won't appear in the output. Usediskutil listto check.
::in-article-ad
::
Automating Routine Tasks
The terminal allows you to create scripts for regular operations. For example, a script to check free space on all disks and send a notification if space is running low:
#!/bin/bash
# check_disk_space.sh
THRESHOLD=10 # free space percentage threshold for warning
df -h | grep -v Filesystem | while read output; do
usage=$(echo $output | awk '{print $5}' | cut -d'%' -f1)
partition=$(echo $output | awk '{print $6}')
if [ $usage -ge $THRESHOLD ]; then
echo "Warning: partition $partition has only $usage% free space left."
fi
done
Save as check_disk_space.sh, make it executable (chmod +x check_disk_space.sh), and run it regularly via cron or launchd.
What's Next?
After mastering the basic commands, you can:
- Explore
man diskutilfor a full description of all options. - Try
diskutil cs(CoreStorage) for managing logical volumes (if using FileVault or older disks). - Use
hdiutilto work with disk images (creating, mounting .dmg files). - Write more complex scripts for backups or monitoring.
Remember: with great power comes great responsibility. Always back up important data before performing disk operations.