Disk Utility in Terminal

We’re going to look at a nice tool built into OSX that we can invoke in the command line, diskutil. In this post, we are going to be looking specifically at partitioning, formatting, and erasing disks.

There are quite a few “verbs” you can use with diskutil. To use a verb, you type it after typing the initial command disktutil, so you type “diskutil verb”

Most of these commands require you to be the owner of the disk you are working on.


list

This will list all your disks in human readable form. That’s it, pretty straight forward. This is probably where you’ll need to start when using DiskUtil.


eject device

This can allow you to unmount a disk, which means you can safely remove it.


verifyPermissions device

If you own a disk, you can use this command to verify permissions – this is the same as verifying permissions with Disk Utility in the GUI.


repairPermissions device

Similar to verifyPermissions, this can repair disk permissions.


secureErase level device

This will erase your entire disk, and can do so with a number of algorithms. This can be really useful if you have a virus like a rootkit, and the only way to remove it is to completely wipe the disk so that there is no recoverable data. This will not partition or format the disk, and until you do so, your disk will be useless after running this command.

There are 5 different levels you can use, as follows:

0 – single-pass zero-fill erase

1 – double-pass zero-fill erase

2 – US DoD 7-pass secure erase

3 – Gutmann algorithm 35-pass secure erase

4 – US DoE 3-pass secure erase


reformat device

This will erase the volume specified and create a new, empty filesystem (of the same type and name as the volume just erased).


partitionDisk device part1format part1name part1size part2format part2name part2size part3format part3name part3size

The disk you select here as the device will be completely wiped out. Partitions will be created as you name them according to “part#name” and their size according to “part#size.” Their format can be specified as any filesystem format, though most likely you will be using HFS+ on a Mac. You can also use “free space” as a formatting option, to not format it and leave the partition completely empty of any type of filesystem.



There is a lot more that you can do with diskutil, so much so that this doesn’t even come close to all the commands. To view them all, check out the manual page.