Last active
September 20, 2024 22:16
-
-
Save ronnycoding/4be2f9681bddf1794abc5c91e86cac3e to your computer and use it in GitHub Desktop.
Dynamic Mac Cache Cleanup Script
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 | |
# Function to get list of installed applications | |
get_installed_apps() { | |
find /Applications -maxdepth 1 -name "*.app" -print0 | xargs -0 -n1 basename | sed 's/\.app$//' | tr '[:upper:]' '[:lower:]' | |
find ~/Applications -maxdepth 1 -name "*.app" -print0 2>/dev/null | xargs -0 -n1 basename | sed 's/\.app$//' | tr '[:upper:]' '[:lower:]' | |
} | |
# Get list of installed apps | |
INSTALLED_APPS=($(get_installed_apps)) | |
# Directories to clean | |
CACHE_DIRS=( | |
"$HOME/Library/Caches" | |
"$HOME/Library/Application Support" | |
"/Library/Caches" | |
) | |
# Function to check if a string contains system-related keywords | |
is_system_related() { | |
local name=$1 | |
[[ "$name" =~ (com\.apple|apple|system|framework|webkit|coreservice) ]] | |
} | |
# Function to check if an app is installed | |
is_app_installed() { | |
local app_name=$(echo "$1" | tr '[:upper:]' '[:lower:]') | |
[[ " ${INSTALLED_APPS[*]} " =~ " ${app_name} " ]] | |
} | |
# Main cleanup function | |
cleanup_caches() { | |
for dir in "${CACHE_DIRS[@]}"; do | |
if [ -d "$dir" ]; then | |
echo "Cleaning $dir" | |
for item in "$dir"/*; do | |
if [ -d "$item" ]; then | |
item_name=$(basename "$item" | tr '[:upper:]' '[:lower:]') | |
if ! is_system_related "$item_name" && ! is_app_installed "$item_name"; then | |
echo "Removing cache for uninstalled app: $item" | |
sudo rm -rf "$item" | |
fi | |
fi | |
done | |
else | |
echo "Directory $dir does not exist, skipping..." | |
fi | |
done | |
} | |
# Run the cleanup | |
echo "Starting cache cleanup..." | |
cleanup_caches | |
echo "Cache cleanup completed." | |
# Optionally, clear system caches | |
echo "Do you want to clear system caches as well? (y/n)" | |
read answer | |
if [[ $answer =~ ^[Yy]$ ]]; then | |
echo "Clearing system caches..." | |
sudo purge | |
echo "System caches cleared." | |
fi | |
echo "Cleanup process finished." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dynamic Mac Cache Cleanup Script
This bash script provides a safe and efficient way to clean up cache files on macOS systems. It dynamically identifies installed applications and removes cache folders for uninstalled applications while preserving system-related caches.
Features
/Applications
and~/Applications
directories~/Library/Caches
,~/Library/Application Support
, and/Library/Caches
purge
commandUsage
dynamic_cleanup_cache.sh
chmod +x dynamic_cleanup_cache.sh
sudo ./dynamic_cleanup_cache.sh
Caution
Requirements
How it works
purge
commandCustomization
You can modify the
CACHE_DIRS
array in the script to include or exclude specific directories for cleanup.Disclaimer
This script is provided as-is, without any warranty. Use at your own risk. The author is not responsible for any data loss or system issues that may occur from using this script.
Contributing
Feel free to fork this script and submit pull requests for any improvements or bug fixes.
License
This script is released under the MIT License. See the LICENSE file for details.