is there a way to eject all external hard drives f

2019-03-24 17:16发布

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.

5条回答
虎瘦雄心在
2楼-- · 2019-03-24 17:25

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

查看更多
混吃等死
3楼-- · 2019-03-24 17:25

You can also use diskutil eject /dev/disk2 or whatever your device number is you want to eject. That worked for me.

查看更多
Rolldiameter
4楼-- · 2019-03-24 17:30

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)'
查看更多
【Aperson】
5楼-- · 2019-03-24 17:35

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

find /dev -name "disk[1-9]" -exec diskutil eject {} \;
查看更多
Anthone
6楼-- · 2019-03-24 17:46

I do it like this:

df | grep Volumes | awk '{ print $1 }' | while read disk; do diskutil unmount "$disk"; done
查看更多
登录 后发表回答