Last active
December 22, 2024 00:25
-
-
Save drego85/f4d36c748195360e542d68659e50f8f7 to your computer and use it in GitHub Desktop.
Clean and Safely Eject macOS Volumes
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 | |
# Utility Script: Clean and Safely Eject macOS Volumes | |
# | |
# This script is designed to to remove unnecessary macOS | |
# system files (e.g., .DS_Store) from USB drives or | |
# SD cards and safely eject selected volume | |
# | |
# It ensures a clean and clutter-free storage device for | |
# use on other systems and provides a streamlined process | |
# for volume management. | |
# | |
# Made with ♥ by Andrea Draghetti | |
# | |
# This file may be licensed under the terms of of the | |
# GNU General Public License Version 3 (the ``GPL''). | |
# | |
# Display currently mounted volumes | |
echo "Mounted volumes:" | |
for volume in /Volumes/*; do | |
echo " - $(basename "$volume")" | |
done | |
printf "\nEnter the name of the USB/SD volume (e.g., SD): " | |
read -r volumename | |
# Check if a volume name was provided | |
if [ -z "$volumename" ]; then | |
# If the input is empty, exit with an error | |
echo "Error: No volume name provided. Exiting." | |
exit 1 | |
fi | |
# Check if the volume exists | |
if df | grep -iw "$volumename" > /dev/null; then | |
# If the volume exists, proceed with cleaning | |
echo "Cleaning unnecessary macOS files in \"/Volumes/$volumename\"..." | |
# Run `dot_clean` to remove unnecessary files and check if it succeeds | |
if sudo dot_clean /Volumes/"$volumename"; then | |
echo "Cleaning completed successfully." | |
else | |
# If `dot_clean` fails, inform the user and exit with an error | |
echo "Error while cleaning the volume." | |
exit 1 | |
fi | |
# Safely eject the volume | |
echo "Ejecting the volume..." | |
if sudo diskutil eject /Volumes/"$volumename"; then | |
# If the ejection succeeds, inform the user | |
echo "Volume successfully ejected!" | |
else | |
# If ejection fails, inform the user and exit with an error | |
echo "Error while ejecting the volume." | |
exit 1 | |
fi | |
# Inform the user that the operation was successful and exit | |
echo "Operation completed successfully!" | |
exit 0 | |
else | |
# If the volume does not exist, inform the user and exit with an error | |
echo "Error: No volume found with the name \"$volumename\". Exiting." | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment