#!/usr/bin/env sh USAGE="ssync-reapr [options] [dirs...] OPTIONS -a AGE (in seconds) limit the reaping of files no older than this seconds old. Default is 86400 (1 day) -n dry run -v verbose logging (use -vv to log reaped files) -h print this message" # HELPER FUNCTIONS verbose_log() { if [ ! -z "$VERBOSE_FLAG" ]; then echo "$@" fi } # OPTIONS AGE_FLAG= AGE_OPT= DRY_RUN_FLAG= VERBOSE_FLAG= while getopts "hnva:" opt; do case "${opt}" in h) echo "$USAGE" exit 1 ;; a) AGE_FLAG=1 AGE_ARG="${OPTARG}" ;; n) DRY_RUN_FLAG=1 ;; v) VERBOSE_FLAG=$(($VERBOSE_FLAG +1)) ;; esac done shift $(($OPTIND -1)) DIRS="$@" # Reaper rm_flag= if [ -z "$DRY_RUN_FLAG" ]; then rm_flag="-delete" if [ $VERBOSE_FLAG -gt 1 ]; then rm_flag="$rm_flag -print" fi else verbose_log "Dry run!" rm_flag="-print" fi target_date= if [ -z "$AGE_FLAG" ]; then target_date=$(date -d "86400 seconds ago" -Is) else target_date=$(date -d "${AGE_ARG} seconds ago" -Is) fi window_flag="-not -newermt ${target_date}" verbose_log "Reaping files older than $target_date: $window_flag" for dir in $DIRS; do target=$(realpath $dir) verbose_log "Clearing files in $target" find $target -type f $window_flag $rm_flag done