15

I want to eject all hard drives with a command on the Terminal instead of going to the Finder and pressing eject on each drive. How can I do it?

4 Answers 4

16

You can use the in-built AppleScript solution, as mentioned in this thread and this page, by adding this to ~/.bash_profile:

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

This will require you giving permission to Terminal to control Finder, or you will get this error:

execution error: Not authorised to send Apple events to Finder. (-1743)

If you want a pure bash solution, here is a function that you can call with ejectall. If you renamed your startup disk or have different Time Machine backups, you may need to edit the condition that filters out the drives.

ejectall() {
    total=0
    ejected=0
    
    for v in /Volumes/*; do
    if [[ $v != *"Macintosh HD" && $v != *"com.apple.TimeMachine"* ]]; then
        echo "Ejecting $v..."
        diskutil eject "$v" # The command to eject the volume
        
        if [ $? -eq 0 ]; then
        ejected=$(($ejected + 1))
        fi
        total=$(($total + 1))
    fi
    done
    
    if [ $total -eq 0 ]; then
    echo "No drives to eject"
    else
    msg="$ejected drive(s) ejected"
    failed=$(($total - $ejected))
    if [ $failed -gt 0 ]; then
        msg="$msg, $failed drive(s) failed to eject"
    fi
    echo $msg
    fi
}

Both methods will also work for CDs.

5
  • 1
    Very nice - I would like to see also a one-liner for mountall - presumably it would have to use diskutil ?
    – Paul R
    Commented Apr 20, 2023 at 14:18
  • @PaulR I don't know as I don't have that problem on macOS. I plug a drive and it mounts.
    – emonigma
    Commented Apr 21, 2023 at 17:20
  • Yes, but there are times when you want to temporarily unmount a disk, so you don’t necessarily unplug it, but then afterwards you need to mount it again. Of course you can unplug it and then plug it back in again, but a simple command would save you the effort and reduce wear and tear on your connectors.
    – Paul R
    Commented Apr 21, 2023 at 21:20
  • 1
    @PaulR Yes, I see now. I checked that diskutil lists the unejected drive, so yes, one can write a similar mountall script. How about writing a new question, tag me and I'll answer it?
    – emonigma
    Commented Apr 23, 2023 at 8:45
  • Good idea: apple.stackexchange.com/q/458990/527
    – Paul R
    Commented Apr 23, 2023 at 9:19
12

Use diskutil.

You can list the current devices with diskutil list, and use diskutil eject device-name to eject a device just like from Finder.

This will go a step further than just using umount by, for example, disconnect a USB device so it /dev/disk node disappears.

See man diskutil for more details.

2
  • Yes, I'd been using diskutil, with the disadvantage of ejecting drives one by one and remembering their names.
    – emonigma
    Commented Sep 19, 2019 at 19:24
  • It will spin down the drive too
    – samus
    Commented Dec 15, 2021 at 16:43
7

I've recently started learning shell scripting so I tried an answer to this as an exercise.

Script uses diskutil list external to get all external disks then loops over the output to unmount them.

I then created an alias in ~/.zshrc so I now only have to type eject in Terminal to eject all external disks attached to my Mac.

(thanks to @nohillside for the tweaks)

#!/bin/sh

#script to eject all external drives
disks=$(diskutil list external | sed -n '/[Ss]cheme/s/.*B *//p')

if [ "$disks" ]
then
echo "$disks" | while read line ; do
    diskutil unmountDisk /dev/$line
  done
else
  echo "No external disks to eject"
fi
0
2

umount has an option to unmount all file systems besides the main one.

sudo umount -A

You can also force this in case files are still busy/locked (with the risk of data loss) by running

sudo umount -A -f
4
  • Nice one, with the benefit of one-line at the cost of a sudo. +1 for ejecting drives that are still busy.
    – emonigma
    Commented Sep 19, 2019 at 19:24
  • I tried this command twice and on both occasions it caused my macOS computer to hang, with the last line in the terminal saying Saving session....
    – emonigma
    Commented Sep 30, 2019 at 9:49
  • @miguelmorin That's a message from Terminal when you terminate the shell running within a tab. I don't see how it relates to unmounting all the drives besides the main drive unless your home folder (or any other file referenced by the shell) is stored on one of the unmounted drives.
    – nohillside
    Commented Sep 30, 2019 at 9:57
  • I don't have the home folder in the unmounted drives. I don't think I have files referenced by the shell there either because they serve for Time Machine backups or a data drive for videos, and I can launch Terminal when they are not connected.
    – emonigma
    Commented Oct 3, 2019 at 9:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .