Last active
August 6, 2023 17:00
-
-
Save pascalpp/27189addbd14afc7cbfdbe6949218332 to your computer and use it in GitHub Desktop.
mountdisks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# mountdisks | |
# script to remount all external drives | |
# assumes your external volumes are all 'APFS Volume' or 'Apple_HFS' | |
# try `diskutil list external` to see your volumes | |
# | |
# Installation | |
# copy this file to /usr/local/bin or somewhere in your path and make it executable | |
# chmod +x path/to/mountdisks | |
# | |
# this script relies on terminal-notifier | |
# brew install terminal-notifier | |
# | |
# Usage: | |
# | |
# mount all external disks | |
# mountdisks | |
# | |
# show what would happen without actually mounting disks | |
# mountdisks --dry-run | |
# | |
dryrun=$(echo $* | grep -e "\b--dry-run\b") | |
if [ "$dryrun" ] | |
then | |
echo "Dry run: no disks will be mounted. Example output shown below." | |
fi | |
# helper method | |
function mountdisks { | |
# reduces diskutil output to a series of lines like 'External HD•disk5s1' | |
# probably a better way to do this with sed - suggestions welcome | |
disks=$(diskutil list external | grep -E '(APFS Volume|Apple_HFS)' | grep -v KB | perl -pe 's/.*(APFS Volume|Apple_HFS) (.*) +\d+\.?\d* [GT]B +(.*)/\2•\3/g' | perl -pe 's/ +•/•/g') | |
if [ "$disks" ] | |
then | |
echo "$disks" | while read line ; do | |
# split the line on • to get diskname and devicename | |
diskname=$(echo $line | cut -d• -f1) | |
devicename=$(echo $line | cut -d• -f2) | |
echo "Mounting $diskname..." | |
if ! [ "$dryrun" ] | |
then | |
diskutil mount /dev/$devicename | |
fi | |
done | |
fi | |
} | |
# execution starts here | |
terminal-notifier -title "Mounting disks" -message "Please wait…" | |
mountdisks | |
EXITCODE=$? | |
if [ "$EXITCODE" -ne "0" ]; then | |
terminal-notifier -title "Oops!" -message "There was a problem mounting some disks." | |
exit $EXITCODE | |
else | |
echo "All disks mounted." | |
terminal-notifier -title "All disks mounted" -message "Get to work." -sound "Glass" | |
# open /Volumes # uncomment to open /Volumes in Finder after mounting disks | |
fi | |
if [ "$dryrun" ] | |
then | |
echo "Dry run complete." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Companion to ejectdisks
Example output: