Created
May 20, 2025 03:07
-
-
Save vndee/462dc3c194d13955a6ad6c399dab1fa1 to your computer and use it in GitHub Desktop.
My mac cleanup utilities.
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 | |
# Colors for better output | |
RED='\033[0;31m' | |
GREEN='\033[0;32m' | |
YELLOW='\033[0;33m' | |
BLUE='\033[0;34m' | |
NC='\033[0m' # No Color | |
# Function to show help | |
show_help() { | |
echo -e "${GREEN}Mac Storage Cleanup Utility${NC}" | |
echo "Usage:" | |
echo " $0 --help Show this help message" | |
echo " $0 --list List large files and folders system-wide" | |
echo " $0 --list-caches List application and system caches" | |
echo " $0 --list-backups List iOS device backups" | |
echo " $0 --list <folder> List large files in specific folder" | |
echo " $0 --delete <path> Safely delete a file or folder (with confirmation)" | |
echo " $0 --clean-caches Clean application caches (with confirmation)" | |
echo " $0 --clean-downloads Clean old downloads (with confirmation)" | |
echo "" | |
echo "Examples:" | |
echo " $0 --list # List all large files and folders" | |
echo " $0 --list ~/Documents # List large files in Documents folder" | |
echo " $0 --delete ~/Downloads/file.dmg # Delete a specific file" | |
echo "" | |
} | |
# Function to list large files and folders system-wide | |
list_large_items() { | |
echo -e "${BLUE}===== Scanning System for Large Files and Folders =====${NC}" | |
# Top 20 largest files | |
echo -e "${YELLOW}Top 20 Largest Files:${NC}" | |
find ~/ -type f -size +50M -not -path "*/Library/Application Support/MobileSync/*" 2>/dev/null | xargs du -h 2>/dev/null | sort -hr | head -20 | |
# Top 20 largest folders | |
echo -e "\n${YELLOW}Top 20 Largest Folders:${NC}" | |
du -h -d 3 ~/ 2>/dev/null | grep -E "[0-9]G" | sort -hr | head -20 | |
# System data breakdown | |
echo -e "\n${YELLOW}System Data Breakdown:${NC}" | |
du -h -d 2 ~/Library/ 2>/dev/null | sort -hr | head -15 | |
echo -e "\n${YELLOW}Developer Folder Contents:${NC}" | |
du -h -d 2 ~/Library/Developer/ 2>/dev/null | sort -hr | head -10 | |
} | |
# Function to list large files in a specific folder | |
list_folder_contents() { | |
local folder="$1" | |
if [ ! -d "$folder" ]; then | |
echo -e "${RED}Error: $folder is not a valid directory${NC}" | |
exit 1 | |
fi | |
echo -e "${BLUE}===== Scanning $folder =====${NC}" | |
# List folders by size | |
echo -e "${YELLOW}Largest Folders in $folder:${NC}" | |
du -h -d 1 "$folder" 2>/dev/null | sort -hr | |
# List files by size | |
echo -e "\n${YELLOW}Largest Files in $folder:${NC}" | |
find "$folder" -type f -size +10M -maxdepth 2 2>/dev/null | xargs du -h 2>/dev/null | sort -hr | head -20 | |
} | |
# Function to list application and system caches | |
list_caches() { | |
echo -e "${BLUE}===== Application and System Caches =====${NC}" | |
echo -e "${YELLOW}Application Caches:${NC}" | |
du -h -d 1 ~/Library/Caches/ 2>/dev/null | sort -hr | |
echo -e "\n${YELLOW}Browser Caches:${NC}" | |
du -h ~/Library/Caches/Google/Chrome/Default/Cache/ 2>/dev/null | sort -hr | head -5 | |
du -h ~/Library/Caches/com.apple.Safari/ 2>/dev/null | sort -hr | head -5 | |
echo -e "\n${YELLOW}System Caches:${NC}" | |
sudo du -h -d 1 /Library/Caches/ 2>/dev/null | sort -hr | |
} | |
# Function to list iOS backups | |
list_backups() { | |
echo -e "${BLUE}===== iOS Device Backups =====${NC}" | |
backup_dir=~/Library/Application\ Support/MobileSync/Backup/ | |
if [ -d "$backup_dir" ]; then | |
echo -e "${YELLOW}iOS Device Backups:${NC}" | |
du -h -d 1 "$backup_dir" 2>/dev/null | sort -hr | |
else | |
echo -e "${YELLOW}No iOS device backups found${NC}" | |
fi | |
} | |
# Function to clean application caches | |
clean_caches() { | |
echo -e "${BLUE}===== Cleaning Application Caches =====${NC}" | |
echo -e "${YELLOW}Warning: This will clean application caches which might affect application performance temporarily.${NC}" | |
echo -e "Do you want to continue? (y/n)" | |
read -r confirm | |
if [ "$confirm" = "y" ]; then | |
echo "Cleaning browser caches..." | |
rm -rf ~/Library/Caches/Google/Chrome/Default/Cache/* 2>/dev/null | |
rm -rf ~/Library/Caches/com.apple.Safari/WebKit/* 2>/dev/null | |
echo "Cleaning application caches..." | |
rm -rf ~/Library/Caches/* 2>/dev/null | |
echo "Caches cleaned successfully" | |
else | |
echo "Operation cancelled" | |
fi | |
} | |
# Function to clean Downloads folder | |
clean_downloads() { | |
echo -e "${BLUE}===== Cleaning Downloads Folder =====${NC}" | |
# Find files older than 30 days | |
old_files=$(find ~/Downloads -type f -mtime +30 2>/dev/null) | |
if [ -z "$old_files" ]; then | |
echo "No old files found in Downloads folder" | |
return | |
fi | |
echo -e "${YELLOW}Files in Downloads older than 30 days:${NC}" | |
find ~/Downloads -type f -mtime +30 -exec ls -lh {} \; 2>/dev/null | sort -k6,7 | |
echo -e "Do you want to delete these files? (y/n)" | |
read -r confirm | |
if [ "$confirm" = "y" ]; then | |
find ~/Downloads -type f -mtime +30 -delete 2>/dev/null | |
echo "Old downloads cleaned successfully" | |
else | |
echo "Operation cancelled" | |
fi | |
} | |
# Function to safely delete a file or folder | |
delete_item() { | |
local item="$1" | |
if [ ! -e "$item" ]; then | |
echo -e "${RED}Error: $item does not exist${NC}" | |
exit 1 | |
fi | |
# Get the size of the item | |
if [ -d "$item" ]; then | |
size=$(du -sh "$item" 2>/dev/null | cut -f1) | |
echo -e "${YELLOW}Warning: You are about to delete folder $item (Size: $size)${NC}" | |
else | |
size=$(du -h "$item" 2>/dev/null | cut -f1) | |
echo -e "${YELLOW}Warning: You are about to delete file $item (Size: $size)${NC}" | |
fi | |
echo -e "${RED}Are you sure you want to delete this? This action cannot be undone! (y/n)${NC}" | |
read -r confirm | |
if [ "$confirm" = "y" ]; then | |
rm -rf "$item" 2>/dev/null | |
echo "Item deleted successfully" | |
else | |
echo "Operation cancelled" | |
fi | |
} | |
# Main logic based on arguments | |
case "$1" in | |
--help) | |
show_help | |
;; | |
--list) | |
if [ -z "$2" ]; then | |
list_large_items | |
else | |
list_folder_contents "$2" | |
fi | |
;; | |
--list-caches) | |
list_caches | |
;; | |
--list-backups) | |
list_backups | |
;; | |
--delete) | |
if [ -z "$2" ]; then | |
echo -e "${RED}Error: No path specified for deletion${NC}" | |
show_help | |
exit 1 | |
else | |
delete_item "$2" | |
fi | |
;; | |
--clean-caches) | |
clean_caches | |
;; | |
--clean-downloads) | |
clean_downloads | |
;; | |
*) | |
echo -e "${RED}Invalid option: $1${NC}" | |
show_help | |
exit 1 | |
;; | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment