~gpanders/pushbroom

ref: b446e071502c18f68229531ab7facd116196300b pushbroom/janitor.sh -rwxr-xr-x 829 bytes
b446e071Greg Anders Update README with options documentation 5 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env bash

# Define which folder to clean out
export TARGET_DIR=$HOME/Downloads

# How long should a file be able to live in the folder?
export DAYS_TO_KEEP=30

# Delete this line if you want to hard-delete files instead of moving them to Trash
export TRASH_DIR=$HOME/.Trash/

# Location to store log files, relative to the Janitor directory
export LOG_DIR=logs

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"' {} \;