Last active
January 29, 2017 11:41
-
-
Save chew-z/309a4f4f473443a09cc3 to your computer and use it in GitHub Desktop.
Script for shrinking polipo cache
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
#!/usr/bin/env zsh | |
# using GNU utils so not completely portable | |
# sort -h [compare human readable numbers] works only with gnu sort | |
# also gfind ... -delete, numfmt are GNU utils | |
# terminal-notifier, polipo - must be in the path | |
# For the reference | |
# gfind -mtime means that the content was modified. This date can be manually changed (e.g. with touch). | |
# gfind -ctime means that either the content was changed, or the file's metadata (permission, owner, etc). | |
# -mtime n | |
# evaluate as true if the file modification time subtracted | |
# from the initialization time, divided by 86400 (with any remainder discarded), is n. | |
# gfind -size - is similarly confusing with skiping reminders | |
cache_dir="/usr/local/var/cache/polipo" | |
declare -i folders_count | |
declare -i cache_size_bytes | |
declare -i cache_size_treshold | |
cd $cache_dir | |
cache_size_treshold=$(numfmt --from=iec 256.0M) | |
cache_size=$(du -hs $cache_dir | gawk {'print $1'}) | |
cache_size_bytes=$(numfmt --from=iec $cache_size) | |
folders_count=$(gfind $cache_dir -type d | wc -l) | |
echo $(date +"%c") | |
echo "cache size: $cache_size ($cache_size_bytes bytes)" | |
echo "$folders_count folders in polipo cache" | |
# if test $folders_count -gt 255 OR | |
if test $cache_size_bytes -gt $cache_size_treshold | |
then | |
# list largest folders (and files if using du without gfind -type d but it is always folders here) | |
du -hc $cache_dir | gsort -h | tail -n 12 | |
# Tell polipo we're messing with the cache | |
killall -s SIGUSR1 polipo | |
sleep 1 | |
# find and remove folders older then 24hrs+ | |
# gfind $cache_dir -maxdepth 2 -type d -ctime +1 | xargs rm -rf | |
# OR | |
# first delete files larger then 10MB (mostly binary downloads like AppStore etc.) | |
gfind $cache_dir -size +10M -delete | |
# then delete files older then 2+ days - looking deeper | |
gfind $cache_dir -maxdepth 3 -type f -ctime +2 -delete | |
# and finally delete folders not modified in 24+ hours | |
# = domains not visited in last 24hrs so probably visited only once while browsing | |
gfind $cache_dir -maxdepth 2 -type d -ctime +1 -exec rm -rf {} + | |
# OR [gnu find only] | |
# gfind $cache_dir -maxdepth 2 -type d -ctime +1 -delete | |
# and tell polipo to clean up | |
polipo -x | |
# All clear, we're done messing with the cache | |
killall -s SIGUSR2 polipo | |
terminal-notifier -title "poliposhrink.sh" \ | |
-message "poliposhrink just trimmed polipo cache. Check log for details." \ | |
-appIcon "$HOME/Pictures/Icons/flat-osx/Osx-Trash-Full.icns" | |
# Check out | |
echo "$(gfind $cache_dir -type d | wc -l) folders left in polipo cache" | |
echo "$(du -hs $cache_dir | gawk {'print $1'}) left in polipo cache" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment