Instantly Eject All Mounted Drives & Disks from the Command Line in Mac OS X

May 22, 2014 - 12 Comments

Eject 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 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.

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: Paul Horowitz in Command Line, Mac OS X, Tips & Tricks

12 Comments

» Comments RSS Feed

  1. Stan-O says:

    Your alias code is invalid, here’s the one that works:
    alias ejectall=’osascript -e ‘”‘”‘tell application “Finder” to eject (every disk whose ejectable is true)'”‘””

  2. Rick says:

    Neither of these works for me. I only get “command not found” as a result. Is there a path setting I have to change? And does it matter if the single and double quotation marks are straight or fancy? Terminal novice here.

    • Paul says:

      The quotations must be straight quotes not curly or fancy. Try just copying and pasting the command as seen on the page, that should do it.

  3. Rick says:

    Nope, still doesn’t work. Anyone using this successfully? Please copy and paste your command here so I can copy. Why does it also say “command not found”? I’ve tried some simple aliases and still get the same error message. Path question?

    • Drew says:

      Or this:

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

    • Paul says:

      What version of OS X are you using? Try typing ‘osascript -blah’ and see if you get an illegal option response

    • Chris says:

      The encoding in stan-o’s solution may not work, depending on your shell/OS.

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

      ^ works for me (OSX 10.8.5, Terminal)

      Keep in mind, if you edit any of your .bash_* files, you have to restart your terminal session before the changes will take effect — i.e., open a new terminal tab, or quit/reopen terminal.

  4. Drew says:

    I did the following from Terminal:

    #Make a new shell script called ejectall.sh in the same directory as the .bash_profile
    nano ejectall.sh

    #Added the following
    osascript -e ‘tell application “Finder” to eject (every disk whose ejectable is true)’

    #Hit control + o to save and control + x to quit nano

    #Make ejectall.sh executable by running the following
    chmod +x ejectall.sh

    Then in the .bash_profile add the following
    alias ejectall=’./ejectall.sh’

    #control + o to save and control + x to quit
    #Restart terminal.

  5. xlymian says:

    alias ejectall=’osascript -e “tell application \”Finder\” to eject (every disk whose ejectable is true)”‘

  6. Boise Ed says:

    I use a little program called Semulov (“volumes” backward). In the menu bar, it lets me eject all or one specific volume.

  7. Alberto says:

    I just select the icons of the volumes on my desktop and type “command + E”, holding it until the volumes are ejected.
    So simpleā€¦

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates