Skip to content

Instantly share code, notes, and snippets.

@phillymjs
Last active August 6, 2020 16:24
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phillymjs/9fd3c3f9455d184005abb5a8e99d7904 to your computer and use it in GitHub Desktop.
Save phillymjs/9fd3c3f9455d184005abb5a8e99d7904 to your computer and use it in GitHub Desktop.
On macOS machines, this script finds orphaned server mounts in /Volumes and removes them. The presence of orphaned mounts can break links to files in Adobe Creative Suite documents and cause other annoyances. Some people in my environment had dozens of them. If you're having issues like this, run this script regularly with a launchdaemon.
#!/bin/bash
# Find directories in /Volumes with .DS_Store files
# See if each directory with a .DS_Store file is currently mounted
# If it is, skip it. If it's not, it's an orphaned mount, so increment the counter
# Fail on undefined variables, since we're running "rm" commands
set -u
orphanCount=0
OIFS="$IFS"
IFS=$'\n'
results=$(/usr/bin/find /Volumes -mindepth 2 -maxdepth 2 -name ".DS_Store")
for ds_store in ${results}
do
share=$(echo ${ds_store}|awk 'BEGIN {FS="/"}; {print $3}')
mountstring=$(mount | grep "${share} (")
if [ -z "${mountstring}" ]; then
let orphanCount=orphanCount+1
rm "${ds_store}"
fi
done
# If /Volumes itself has a .DS_Store, decrement the result by 1 so it's an accurate count
# Commented out because adding -mindepth 2 to the find command ignores /Volumes/.DS_Store
# making this bit unnecessary
#if [ -f "/Volumes/.DS_Store" ];
#then
#let orphanCount=orphanCount-1
#fi
# Now we know the orphans are empty, so let's delete them
/usr/bin/find /Volumes -maxdepth 1 -empty -exec /bin/rmdir {} \;
IFS="$OIFS"
# Report the results
echo "${orphanCount} orphaned mounts found and removed."
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment