is there a way to eject all external hard drives from the command line? (OS X)

12,999

Solution 1

In Terminal try:

  • umount -a (All the filesystems described via getfsent(3) are unmounted.)
  • umount -A (All the currently mounted filesystems except the root unmounted.)

Fore more information see man umount.

Update:

Seems like you can also use this:

diskutil unmountDisk /dev/disk*

Didn't test it, though. If it doesn't work, try to use "unmount" instead of "unmountDisk".

Oh, I also found the eject argument (instead of unmountDisk). That might also be of interest.

Update 2:

diskutil eject /dev/* seems what you are looking for (see comments).

Solution 2

There is another elegant way to unmount all external hard drives without knowing the exact names:

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

To ignore network mounts and optical disks, use:

osascript -e 'tell application "Finder" to eject (every disk whose ejectable is true and local volume is true and free space is not equal to 0)'

Solution 3

I found this to work for ejecting all dmg and physical hard drives:

find /dev -name "disk[1-9]" -exec diskutil eject {} \;

Solution 4

I do it like this:

df | grep Volumes | awk '{ print $1 }' | while read disk; do diskutil unmount "$disk"; done
Share:
12,999
dan
Author by

dan

Updated on June 05, 2022

Comments

  • dan
    dan about 2 years

    Is there a way to eject all the mounted hard drive volumes on an OS X computer from the command line? Applescript is OK if I can wrap that in a shell script.

  • BastiBen
    BastiBen over 14 years
    Why not? Any error messages? If so, please post them here. Also did you try the "-A" option (capital A).
  • dan
    dan over 14 years
    I tried umount -a, and a finder window opened, but that was it. I'll have to try the other commands tonight when I'm back where my external hard drives are.
  • Barry Jones
    Barry Jones almost 7 years
    This is probably the most correct way to do it, even though the syntax is horrific.
  • Ondrej Galbavý
    Ondrej Galbavý almost 7 years
    diskutil eject also stops spinning of external HDD. diskutil unmountDisk keeps it spinning.