Skip to content

Instantly share code, notes, and snippets.

@JannikArndt
Last active July 30, 2018 02:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JannikArndt/feb720c1f5d210b4820b880af23f2a07 to your computer and use it in GitHub Desktop.
Save JannikArndt/feb720c1f5d210b4820b880af23f2a07 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# inspired by
# https://github.com/fwartner/mac-cleanup/blob/master/cleanup.sh
# https://gist.github.com/jamesrampton/4503412
# https://github.com/mengfeng/clean-my-mac/blob/master/clean_my_mac.sh
# https://github.com/szymonkaliski/Dotfiles/blob/master/Scripts/clean-my-mac
# http://brettterpstra.com/2015/10/27/vacuuming-mail-dot-app-on-el-capitan/ / https://github.com/pbihq/tools/blob/master/MailDBOptimiser.sh
bytesToHuman() {
b=${1:-0}; d=''; s=0; S=(Bytes {K,M,G,T,E,P,Y,Z}iB)
while ((b > 1024)); do
d="$(printf ".%02d" $((b % 1024 * 100 / 1024)))"
b=$((b / 1024))
let s++
done
echo "$b$d ${S[$s]} of space was cleaned up :3"
}
# Check if Time Machine is running
if [ `tmutil status | grep -c "Running = 1"` -ne 0 ]; then
echo "Time Machine is currently running. Let it finish first!"
exit
fi
# Check for last Time Machine backup and exit if it's longer than 1 hour ago
lastBackupDateString=`tmutil latestbackup | grep -E -o "[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{6}"`
if [ "$lastBackupDateString" == "" ]; then
read -n 1 -p "$(tput setaf 3)Last Time Machine backup cannot be found. Proceed anyway?$(tput sgr0) (y/n) " RESP
echo ""
if [ "$RESP" != "y" ]; then
exit
fi
else
lastBackupDate=`date -j -f "%Y-%m-%d-%H%M%S" $lastBackupDateString "+%s"`
if [ $((`date +%s` - $lastBackupDate)) -gt 3600 ]
then
printf "Time Machine has not backed up since `date -j -f %s $lastBackupDate` (more than 60 minutes)!"
exit 1003
else
echo "Last Time Machine backup was on `date -j -f %s $lastBackupDate`. "
fi
fi
oldAvailable=$(df / | tail -1 | awk '{print $4}')
sudoPW="" # filled if needed
read -n 1 -p "$(tput setaf 3)Update and clean Homebrew?$(tput sgr0) (y/n) " RESP
echo ""
if [ "$RESP" = "y" ]; then
brew update
brew upgrade
brew prune
brew cleanup --force -s
brew cask cleanup
rm -rf /Library/Caches/Homebrew/*
brew tap --repair
brew doctor
fi
read -n 1 -p "$(tput setaf 3)Run periodic scripts? (requires sudo)$(tput sgr0) (y/n) " RESP
echo ""
if [ "$RESP" = "y" ]; then
if [ "$sudoPW" = "" ]; then read -s -p "Enter Password for sudo: " sudoPW; fi
echo $sudoPW | sudo -S periodic daily weekly monthly
fi
read -n 1 -p "$(tput setaf 3)Clean .DS_Store files in ~? (requires sudo)$(tput sgr0) (y/n) " RESP
echo ""
if [ "$RESP" = "y" ]; then
if [ "$sudoPW" = "" ]; then read -s -p "Enter Password for sudo: " sudoPW; fi
echo $sudoPW | sudo -S find ~ / -name ".DS_Store" -exec rm {} \;
fi
read -n 1 -p "$(tput setaf 3)Vacuum Mail Envelope Index?$(tput sgr0) (y/n) " RESP
echo ""
if [ "$RESP" = "y" ]; then
osascript -e 'tell application "Mail" to quit'
sizebefore=$(du -h ~/Library/Mail/V4/MailData/"Envelope Index" | awk '{ print $1 }')B
sqlite3 ~/Library/Mail/V4/MailData/Envelope\ Index vacuum
sizeafter=$(du -h ~/Library/Mail/V4/MailData/"Envelope Index" | awk '{ print $1 }')B
printf "\nMail index size before:\t%s\nMail index size after:\t%s\n\n" "$sizebefore" "$sizeafter"
osascript -e 'tell application "Mail" to activate'
fi
read -n 1 -p "$(tput setaf 3)Clean system caches? (requires sudo)$(tput sgr0) (y/n) " RESP
echo ""
if [ "$RESP" = "y" ]; then
if [ "$sudoPW" = "" ]; then read -s -p "Enter Password for sudo: " sudoPW; fi
echo $sudoPW | sudo -S rm -rf /Library/Caches/*
echo $sudoPW | sudo -S rm -rf ~/Library/Caches/*
fi
read -n 1 -p "$(tput setaf 3)Clean application caches?$(tput sgr0) (y/n) " RESP
echo ""
if [ "$RESP" = "y" ]; then
for x in $(ls ~/Library/Containers/)
do
rm -rfv ~/Library/Containers/$x/Data/Library/Caches/*
# echo "~/Library/Containers/$x/Data/Library/Caches"
done
fi
read -n 1 -p "$(tput setaf 3)Empty Trash?$(tput sgr0) (y/n) " RESP
echo ""
if [ "$RESP" = "y" ]; then
rm -rfv ~/.Trash/*
fi
read -n 1 -p "$(tput setaf 3)Clean logs? (requires sudo)$(tput sgr0) (y/n) " RESP
echo ""
if [ "$RESP" = "y" ]; then
if [ "$sudoPW" = "" ]; then read -s -p "Enter Password for sudo: " sudoPW; fi
echo $sudoPW | sudo -S rm -rfv /private/var/log/*
echo $sudoPW | sudo -S rm -rfv /Library/Logs/*
echo $sudoPW | sudo -S rm -rfv ~/Library/Logs/*
echo $sudoPW | sudo -S rm -rfv ~/Library/Application\ Support/Adobe/Common/Media\ Cache\ Files/*
echo $sudoPW | sudo -S rm -rfv ~/Library/Containers/com.apple.mail/Data/Library/Logs/Mail/*
fi
read -n 1 -p "$(tput setaf 3)Clean temporary files?$(tput sgr0) (y/n) " RESP
echo ""
if [ "$RESP" = "y" ]; then
if [ "$sudoPW" = "" ]; then read -s -p "Enter Password for sudo: " sudoPW; fi
echo $sudoPW | sudo -S rm -rfv /tmp/*
echo $sudoPW | sudo -S rm -rfv /private/var/tmp/Processing/
echo $sudoPW | sudo -S rm -rfv /private/var/tmp/Xcode/
echo $sudoPW | sudo -S rm -rfv /private/var/tmp/tmp*
fi
read -n 1 -p "$(tput setaf 3)Clean iOS Applications?$(tput sgr0) (y/n) " RESP
echo ""
if [ "$RESP" = "y" ]; then
rm -rfv ~/Music/iTunes/iTunes\ Media/Mobile\ Applications/*
fi
read -n 1 -p "$(tput setaf 3)Clean iOS Device Backups?$(tput sgr0) (y/n) " RESP
echo ""
if [ "$RESP" = "y" ]; then
rm -rfv ~/Library/Application\ Support/MobileSync/Backup/*
fi
read -n 1 -p "$(tput setaf 3)Clean XCode Derived Data and Archives?$(tput sgr0) (y/n) " RESP
echo ""
if [ "$RESP" = "y" ]; then
rm -rfv ~/Library/Developer/Xcode/DerivedData/*
rm -rfv ~/Library/Developer/Xcode/Archives/*
fi
read -n 1 -p "$(tput setaf 3)Clean any old versions of gems?$(tput sgr0) (y/n) " RESP
echo ""
if [ "$RESP" = "y" ]; then
gem cleanup
fi
read -n 1 -p "$(tput setaf 3)Clean 'Open With…' (restarts Finder)?$(tput sgr0) (y/n) " RESP
echo ""
if [ "$RESP" = "y" ]; then
/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user
killall Finder
fi
read -n 1 -p "$(tput setaf 3)Remove dangling docker containers? (docker must be running!)$(tput sgr0) (y/n) " RESP
echo ""
if [ "$RESP" = "y" ]; then
docker rm $(docker ps -q -f status=exited)
fi
echo 'Purge inactive memory...'
if [ "$sudoPW" = "" ]; then read -s -p "Enter Password for sudo: " sudoPW; fi
echo $sudoPW | sudo purge
read -n 1 -p "$(tput setaf 3)Rebuild Spotlight? (takes a long time!)$(tput sgr0) (y/n) " RESP
echo ""
if [ "$RESP" = "y" ]; then
if [ "$sudoPW" = "" ]; then read -s -p "Enter Password for sudo: " sudoPW; fi
echo $sudoPW | sudo mdutil -E /
fi
echo 'Success!'
newAvailable=$(df / | tail -1 | awk '{print $4}')
count=$((newAvailable-oldAvailable))
count=$(( $count * 512))
bytesToHuman $count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment