Created
February 27, 2021 07:04
-
-
Save apizz/3599d089c873a6ec43e1e1ba08520384 to your computer and use it in GitHub Desktop.
Given an autopkg cache path, will remove any download and pkg items older than X days
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 | |
# Script to find and delete .dmg, .pkg, .tar.gz, and .zip files from | |
# your autopkg cache | |
# Failed / incomplete downloads (prefixed with tmp) are also removed | |
# Number of days after which to delete matching files | |
readonly DAYS_TO_DELETE=7 | |
# Path to autopkg Cache folder for current user | |
readonly SEARCH_PATH="/Users/${USER}/Library/AutoPkg/Cache" | |
# List of search terms to exclude from cleanup, separated by spaces | |
# | |
# readonly EXCLUSIONS="Slack Firefox" | |
# Path to autopkg cleanup log. For CLI use, otherwise comment out | |
readonly LOG="/Library/Logs/autopkg_cleanup.log" | |
####################################### | |
# Output errors with timestamp to STDERR | |
# Globals: | |
# CLEANUP_LIST | |
# DAYS_TO_DELETE | |
# SEARCH_PATH | |
# EXCLUSIONS | |
# Arguments: | |
# None | |
# Outputs: | |
# None | |
####################################### | |
get_files_to_clean() { | |
CLEANUP_LIST="$(/usr/bin/find ${SEARCH_PATH} \( -iname "*.pkg" -o -iname "*.dmg" -o -iname "*.zip" -o -iname "tmp*" -o -iname "*.tar.gz" \) -mtime +${DAYS_TO_DELETE} -maxdepth 5 -print | sort)" | |
# Remove any excluded items from the list | |
for i in $EXCLUSIONS; do | |
CLEANUP_LIST=$(echo "$CLEANUP_LIST" | grep -v ${i}) | |
done | |
} | |
####################################### | |
# Output results | |
# Globals: | |
# CLEANUP_LIST | |
# DAYS_TO_DELETE | |
# Arguments: | |
# None | |
# Outputs: | |
# Prints & writes to log if no files found older | |
# than specified DAYS_TO_DELETE, or prints & writes | |
# all found files to clean and indicates if it failed | |
####################################### | |
clean_files() { | |
# Output results | |
if [ -z "$CLEANUP_LIST" ]; then | |
# Indicate no files found to delete | |
writelog "No files found to clean older than ${DAYS_TO_DELETE} days." | |
else | |
# Indicate found files to delete | |
writelog "Files found to clean older than ${DAYS_TO_DELETE} days:" | |
for i in ${CLEANUP_LIST}; do | |
writelog "${i}" | |
done | |
# Remove found files and output any failures | |
for f in $CLEANUP_LIST; do | |
writelog "Removing $(basename ${f}...)" | |
/bin/rm -f "$f" || writelog "Failed to remove ${f}." | |
done | |
fi | |
} | |
start_run() { | |
writelog "####### START $(/bin/date +'%Y-%m-%d') #######" | |
} | |
end_run() { | |
writelog "####### END $(/bin/date +'%Y-%m-%d') #######" | |
} | |
writelog() { | |
# For LaunchDaemon use | |
#echo "$(/bin/date +'%Y-%m-%dT%H:%M:%S') - $*" #>&1 | |
# For CLI use | |
echo "$(/bin/date +'%Y-%m-%dT%H:%M:%S') - $*" >> "$LOG" | |
} | |
main() { | |
start_run | |
get_files_to_clean | |
clean_files | |
end_run | |
} | |
main "@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment