Skip to content

Instantly share code, notes, and snippets.

@pascalpp
Last active August 6, 2023 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pascalpp/27189addbd14afc7cbfdbe6949218332 to your computer and use it in GitHub Desktop.
Save pascalpp/27189addbd14afc7cbfdbe6949218332 to your computer and use it in GitHub Desktop.
mountdisks
#!/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
@pascalpp
Copy link
Author

pascalpp commented Aug 6, 2023

Companion to ejectdisks

Example output:

❯ mountdisks 
Mounting Wrangler...
Volume Wrangler on /dev/disk5s2 mounted
Mounting Tonka...
Volume Tonka on /dev/disk7s1 mounted
Mounting Spree...
Volume Spree on /dev/disk8s2 mounted
Mounting Frog Ville...
Volume Frog Ville on /dev/disk10s1 mounted
All disks mounted.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment