#!/usr/bin/env bash
# Define which folder to clean out (default: ~/Downloads)
export TARGET_DIR=$1
# How many days should a file be able to live in the folder? (default: 30)
export DAYS_TO_KEEP=$2
# Trash folder to move deleted files to (default: ~/.Trash)
export TRASH_DIR=$3
# Location to store log files, relative to the Janitor directory (default: logs)
export LOG_DIR=logs
if [ -z "$TARGET_DIR" -o -z "$DAYS_TO_KEEP" ]; then
echo "Minimum 2 arguments required"
exit 1
fi
function clean() {
if [ $# -eq 0 ]; then
exit 1
fi
if [ ! -z "$TRASH_DIR" ]; then
rsync -a "$1" "$TRASH_DIR"
fi
rm -rf "$1"
LOG="$LOG_DIR"/janitor_$(date +"%Y%m%d").log
if [ ! -e $LOG ]; then
echo "Files removed from $TARGET_DIR on $(date +%m-%d-%Y):" > "$LOG"
fi
echo "${1##*/}" >> "$LOG"
}
export -f clean
find "$TARGET_DIR" -mtime +$DAYS_TO_KEEP -depth 1 -exec bash -c 'clean "$0"' {} \;