Eject all mounted volumes from the command line of Mac OS XEject all mounted volumes from the command line of Mac OS X The next time you’re at the command line and need to eject every single mounted volume, hard drive, disk, disk image, and/or external drive attached to a Mac, you can instantly eject them all in one fell swoop with a handy osascript command string. This is great if you work frequently in the Terminal and you’re wanting to quickly pack up a workstation and head out, but it’s also very useful for remotely managing Macs through an ssh connection, or adding to a shell script, amongst other potential uses.

For those who are unfamiliar with osascript, it’s a command line interface to AppleScript that allows you to execute AppleScripts and OSA language scripts from the terminal. The functionality is quite simple, you basically just feed it a script or statement that you’d otherwise place into the AppleScript Editor, and the whole thing is handled from the terminal rather than having to launch into the GUI app within OS X. Let’s use osascript to eject all the mounted volumes on a Mac.

Ejecting All Mounted Volumes, Drives, and Disk Images via Terminal

From the Terminal, run the following command string on a single line:

osascript -e 'tell application "Finder" to eject (every disk whose ejectable is true)'

As usual with the command line, be sure the entire command syntax is on one line. Unless your terminal window is set very large it will likely wrap, that’s ok.

Eject all volumes using the command line in Mac OS XEject all volumes using the command line in Mac OS X

The moment you hit the enter key to run the command, volumes will start ejecting. Disk images and network volumes go immediately, while external spinning hard drives will spin up first before ejecting. Nonetheless, even if you have to wait for the spin up of some drives the entire task is very fast and there is no further interaction required.

There are certainly other ways to do this, including with the hdiutil and diskutil tools, but the osascript method is probably the fastest since it ejects everything without having to use mount points. If you happen to know of another method to mass eject volumes, perhaps one that is cross platform compatible so that it’d work in Mac OS X as well as linux, do let us know in the comments.

Find yourself using this often? Consider adding it to your bash_profile with an alias to shorten the length of the command. Simply append something like the following to .bash_profile for this purpose:

alias ejectall='osascript -e 'tell application "Finder" to eject (every disk whose ejectable is true)''

This allows you only have to type ‘ejectall’ rather than the entire command string.

Of course, this is all a bit advanced, and most Mac users are better served ejecting disks by holding down the eject key, or going through the OS X Finder by finding them in the sidebar, hovering over the name, and clicking the eject button.

Source

Follow Me:
Top Best Sellers!!