Skip to main content

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.

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

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.

added Time Machine backups
Source Link
emonigma
  • 1.1k
  • 1
  • 10
  • 25

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 Mobile Backupsbackups, 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"
          
          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.

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 Time Machine Mobile 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" ]]; then
          echo "Ejecting $v..."
          diskutil eject "$v"
          
          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.

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

added 131 characters in body
Source Link
emonigma
  • 1.1k
  • 1
  • 10
  • 25

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 Time Machine Mobile 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" ]]; then
          echo "Ejecting $v..."
          diskutil eject "$v"
          
          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.

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 Time Machine Mobile 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" ]]; then
        echo "Ejecting $v..."
        diskutil eject "$v"

        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
}

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 Time Machine Mobile 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" ]]; then
          echo "Ejecting $v..."
          diskutil eject "$v"
          
          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.

Source Link
emonigma
  • 1.1k
  • 1
  • 10
  • 25
Loading