Last active
June 6, 2023 12:32
-
-
Save septicwolf818/3bdec287a812a3bbb28eed6598d6a780 to your computer and use it in GitHub Desktop.
Bash script to update brew packages and clean logs/cache on MacOS
This file contains 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 | |
# List of directories to clean | |
directories=( | |
# Caches | |
"/Library/Caches:Yes" | |
"$HOME/Library/Caches:No" | |
# Logs | |
"$HOME/Library/Logs:No" | |
"/var/log:Yes" | |
"/System/Library/Caches:Yes" | |
# Var files | |
"/private/var/folders:Yes" | |
# Crash reports | |
"/Library/Application Support/CrashReporter:Yes" | |
) | |
# Function to prompt for user confirmation | |
confirm() { | |
read -p $'\n❓ Do you want to clean '"$1"?' (y/n): ' choice | |
case "$choice" in | |
y|Y ) return 0;; | |
n|N ) return 1;; | |
* ) confirm "$1";; | |
esac | |
} | |
# Function to prompt for admin password if necessary | |
ask_admin_password() { | |
sudo -v | |
while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null & | |
} | |
# Function to check if Homebrew is installed | |
check_homebrew() { | |
if ! command -v brew >/dev/null 2>&1; then | |
echo "❌ Homebrew is not installed. Skipping Homebrew commands." | |
return 1 | |
fi | |
return 0 | |
} | |
# Function to clean Homebrew unused dependencies | |
clean_homebrew() { | |
ask_admin_password | |
echo "🧹 Cleaning Homebrew..." | |
brew cleanup | |
echo "✨ Homebrew cleaned." | |
} | |
# Function to update Homebrew package lists and update packages | |
update_homebrew() { | |
echo "🔄 Updating Homebrew package lists..." | |
brew update | |
echo "✨ Homebrew package lists updated." | |
echo "🔄 Updating Homebrew packages..." | |
brew upgrade | |
echo "✨ Homebrew packages updated." | |
} | |
# Function to update Homebrew casks | |
update_casks() { | |
echo "🔄 Updating Homebrew casks..." | |
brew upgrade --cask | |
echo "✨ Homebrew casks updated." | |
} | |
# Function to clean directories | |
clean_directories() { | |
date > removed.txt | |
for ((i=0; i<${#directories[@]}; i++)); do | |
dir_info="${directories[$i]}" | |
dir="${dir_info%%:*}" | |
sudo_required="${dir_info##*:}" | |
current_dir_name="${dir%%:*}" | |
current_dir=$((i + 1)) | |
if [[ -d "$dir" ]]; then | |
if confirm "$dir"; then | |
echo "🔄 Cleaning $dir" | |
if [[ "$sudo_required" == "Yes" ]]; then | |
ask_admin_password | |
sudo rm -rfv "$dir"/* >> removed.txt 2> /dev/null | |
else | |
rm -rfv "$dir"/* >> removed.txt 2> /dev/null | |
fi | |
echo "✅ Cleaned $dir" | |
else | |
echo "⏭️ Skipped $dir" | |
fi | |
else | |
echo "❌ Directory not found: $dir" | |
fi | |
done | |
} | |
# # Update Homebrew if installed | |
if check_homebrew; then | |
echo -e "\n🍺 UPDATING HOMEBREW\n" | |
update_homebrew & | |
pid=$! | |
wait $pid | |
echo -e "\n✨ HOMEBREW UPDATED\n" | |
fi | |
# # Update Homebrew casks if installed | |
if check_homebrew; then | |
echo -e "\n🍺 UPDATING HOMEBREW CASKS\n" | |
update_casks & | |
pid=$! | |
wait $pid | |
echo -e "\n✨ HOMEBREW CASKS UPDATED\n" | |
fi | |
# # Clean Homebrew if installed | |
if check_homebrew; then | |
echo -e "\n🍺 CLEANING HOMEBREW\n" | |
clean_homebrew & | |
pid=$! | |
wait $pid | |
echo -e "\n✨ HOMEBREW CLEANED\n" | |
fi | |
# Run cleaning | |
echo -e "\n🧽 CLEANING DIRECTORIES" | |
clean_directories | |
echo -e "\n✨ DIRECTORIES CLEANED" | |
echo -e "\nℹ️ Paths of removed files are saved to removed.txt" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment