Skip to content

Instantly share code, notes, and snippets.

@higgins

higgins/clean Secret

Created April 8, 2021 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save higgins/b825103ce0bcf3fbefc79a43921244b5 to your computer and use it in GitHub Desktop.
Save higgins/b825103ce0bcf3fbefc79a43921244b5 to your computer and use it in GitHub Desktop.
Clean -- remove stale files in commonly dirty directories. "stale" means anything not viewed in over 1 week
#! /bin/bash
#
# PURPOSE:
# Remove stale files in common directories. "stale" means haven't
# viewed in over 1 week. System must write atime
#
# NOTES:
# - check `mount` to confirm atime is set. for my personal machines it
# will be. we aint launchin rockets!
#
# FIXME:
# - test it
#
# TODO:
# - parameterize `staleness`
# - warn before execution
# - report before/after execution
#
# REFS:
# https://unix.stackexchange.com/questions/8840/last-time-file-opened
# https://unix.stackexchange.com/questions/9496/looping-through-files-with-spaces-in-the-names
#
# Disable spotlight indexing
# http://osxdaily.com/2011/12/30/exclude-drives-or-folders-from-spotlight-index-mac-os-x/
declare -a LINE_OF_SIGHT=(~/Desktop ~/Downloads ~/Documents)
OLD_IFS="$IFS"
IFS=$'\n'
# iterate through each of our "line of sight" directories (any
# directory we want to keep clean) and delete those we haven't
# accessed in over a week. Disable indexing of these directories
for dirtyDir in "${LINE_OF_SIGHT[@]}"
do
find $dirtyDir -atime +1w -type f -delete
done
# Take out the trash
rm -rf ~/.Trash/*
# If docker is on system, prune unused images
if [ -x "$(command -v docker)" ]; then
echo "Pruning unused docker images"
docker system prune -f
fi
# teardown
IFS="$OLD_IFS"
@higgins
Copy link
Author

higgins commented Nov 16, 2022

comment out the docker prune bits if that's too aggressive for you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment