Access hidden Disk Utility commands using Terminal in macOS

How to use the diskutil command from the Terminal in macOS on the Apple Mac. Use it to format disks, mount and unmount them, erase them and more.

Disk Utility on the Apple Mac is a useful tool for formatting disks, partitioning them, and so on. However, there are many more features available from the command line. Learn the secrets!

For most tasks involving formatting, partitioning and erasing disks, the Disk Utility tool in the Applications/Utilities folder is recommended. The graphical user interface makes accessing the features and functions straightforward and easy.

There is another side to Disk Utility and the diskutil command is where the real power lies. It has a much wider range of functions and is a lot more powerful. It does everything in Disk Utility and a lot more.

In this article I will show how to use some of the basic functions of the diskutil command and show where you can get more information about it if you want to use some of the more powerful features.

List the disks with diskutil

Open Terminal in the Applications/Utilities folder and type diskutil list

The diskutil command in macOS used to list the disks in the Apple Mac

The information you see may not look like mine because I have partitioned the internal boot disk and have installed Windows 10 using Boot Camp. I also have a USB drive that is split into two partitions. Most people's diskutil list display will probably look a lot simpler than mine.

There are four highlighted boxes in the screenshot above. The first shows the physical drives and at the top is disk0 (internal, physical) and at the bottom is disk2 (external, physical).

These are the internal and external drives I have. The disk1 (internal, virtual) is no doubt because of the way Boot Camp works. If you don’t have Boot Camp you probably won’t see a virtual disk.

The second box highlights the name of each disk. On disk0 is:

EFI - disk partitioning system area. Ignore it.
MacBook - the name of the disk volume on the internal disk
Recovery HD - used when booting the Mac and using recovery tools
BOOTCAMP - Windows 10 is installed on this Mac

The external USB drive is called disk2 and at the bottom it has EFI, Disk1, Recovery HD and Disk2. The drive is partitioned into two and I named them Disk1 and Disk2. These are the names under the disk icons on the desktop. It is a bad choice for volume names when using diskutil from the command line because diskutil names disks disk0, disk1, disk2 and so on.

The third column shows the disk size *500.1GB for the internal disk and *500.1GB for the external USB disk. Below each disk are the sizes of each partition.

Using the name and the size columns, you can work out which disk is which - internal, external, which partition and so on.

When using the diskutil command, you use the name in the IDENTIFIER column on the right. So disk2  at the bottom of the identifier column refers to my external USB drive and disk2s4 refers to a partition on it. The name is irrelevant, it is the identifier that is important.

Show detailed disk information

Suppose I want a detailed description of the second partition on my external USB disk drive. The identifier is disk2s2 and so the command is diskutil info disk2s2

Use diskutil in macOS to get information about disk drives

There is a lot of information there like the media type, SMART status if available, the filing system, size, whether macOS can be installed on it and so on.

Check the disk for errors

There are commands for verifying that a volume is OK and for repairing faults:

diskutil verifyVolume x
diskutil repairvolume x

x = the volume/partition identifier, such as disk2s2 or disk2s4.

Check that the disk is OK with the diskutil command in macOS

On old versions of OS X you could also use diskutil verifyPermissions x and diskutil repairPermissions x, but since El Capitan this feature has been removed. Apple says that permissions are protected and cannot be modified by the user. They should not need checking or repairing.

To check that the disk is OK, in other words, that the partitions are correctly defined, and to repair faults use:

diskutil verifyDisk y
diskutil repairDisk y

y = the disk, not the volume. So in my case it would be disk2, which is the identifier for the USB drive.

Mount and umount volumes and disks

A disk that is internal or external can be seen by the Mac, but it cannot be accessed until it is mounted. This makes it visible in Finder and allows files to be read and written to it. If a disk is unmounted it is removed from Finder and is no longer available for reading or writing. The Mac still knows it is there, you just can’t access it till it is mounted again.

diskutil mount x
diskutil unmount x

x = the partition identifier, such as disk2s2.

A disk can contain 1, 2 or more partitions. Use these commands to mount all of them or to unmount all of them in one go:

diskutil mountDisk y
diskutil unmountDisk y

y = the disk, not the volume. So in my case it would be disk2, which is the identifier for the USB drive.

I unmounted the USB disk and then mounted it again. For some reason the EFI partition was mounted, but you don’t want to mess around with that because it is for the system only, so I unmounted just that partition.

Mount and unmount disks using diskutil in macOS

Delete all files on a disk

If you want to erase all the files and folders on a volume (a partition), then use:

diskutil reformat x

x is the volume (partition) identifier. It formats the volume with the same format that is already used. It is a bit like a reset command and the result is an empty disk ready to store your files.

Read the manual

There are many more commands and the list of actions that you can perform is long and complicated. Hopefully, this brief look at some of the simpler functions of diskutil gives you enough knowledge to understand the manual, which can appear to be very confusing at first sight. However, it isn’t as hard as it looks.

Just remember that some commands expect you to specify a disk, like disk0, disk1, disk2 and so on, but others expect a volume (partition). So disk2 is the whole disk, disk2s1 is the first partition, disk2s2 is the second and so on.

At the Terminal command prompt, enter man and press enter. Type diskutil and a brief description of every function is listed.

At the Terminal, type man diskutil to see a complete description of every function in great detail. Press Q to quit the manual.

The diskutil manual in a Terminal window in macOS on the Apple Mac

Warning

Thoughout this article I have used diskutil with an external USB drive because it is safer than trying out commands on the internal boot disk. You don't want to accidentally format or erase the boot disk! Double check that you have specified the right disk or volume before pressing enter to run a command! Make sure you have a backup of the boot disk too.

 

Share

Add new comment

By submitting this form, you accept the Mollom privacy policy.

Related items you will like...