How to mount and unmount drives in macOS and OS X from the command line

Unmounting external drives on a Mac is usually done quick and simple by either dragging drive icon to the trash, or by using the eject symbol in a Finder window. Mounting usually happens automatically when a new drive is inserted into a USB port or SD card slot.

However, there is a way to do this via the command line, of which I am a big fan. Fire up a Terminal session and see how to do it.

Listing available drives

To see what’s currently attached to your Mac, let’s use the diskutil command, followed by the word list. You’ll see output like this:

Attached drives are listed with their physical locations on the left (i.e. /dev/disk0, /dev/disk1, etc), as well as with their respective partitions if available on the right (like disk0s1, disk1s2, etc). Make a mental note of the latter: you’ll see that we have a physical disk (like disk0), on which several partitions may have been created. It is those partitions we’ll mount and unmount, NOT the physical drive.

Unmounting an attached hard drive

On my system I have two internal hard disks (disk0 and disk1), and one external USB drive (disk2). Let’s unmount that USB drive now:

Note how we use the unmount command. We need to specify the location of the partition with its full path (i.e. /dev/disk2s1).

Mounting an attached hard drive

To mount the drive again, without having to take it out and plugging it in again, I can issue this command:

Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or draw on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.

2 thoughts on “How to mount and unmount drives in macOS and OS X from the command line

  1. Hi, Jay! How can I execute unmount script when I close the lid (sleep) of my mac and execute mount script when I open the lid (wakeup)?

    1. Hi Barry – oh, that’s a good question. I wish I knew, it would be a very useful option indeed. Sadly I don’t know the answer, but let’s keep our eyes and ears open. If I find a way to do it, I’ll let you know. If you find a solution, please share it here.

      All the best,

      JAY

Add your voice!