~gpanders/pushbroom

ref: bb33aba93b936c812646ca44f20f10348c89b032 pushbroom/bin/janitor -rwxr-xr-x 1.0 KiB
bb33aba9Greg Anders Use proper find syntax 3 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
36
37
38
39
40
#!/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 of Janitor installation
export JANITOR_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd)"

if [ -z "$TARGET_DIR" -o -z "$DAYS_TO_KEEP" ]; then
    echo "Minimum 2 arguments required"
    exit 1
fi

function sweep() {
    if [ $# -eq 0 ]; then
        exit 1
    fi

    if [ ! -z "$TRASH_DIR" ]; then
        rsync -a "$1" "$TRASH_DIR" 2>/dev/null
    fi
    rm -rf "$1" 2>/dev/null

    LOG="$JANITOR_DIR/logs/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 sweep

find "$TARGET_DIR" -mindepth 1 -maxdepth 1 -mtime +$DAYS_TO_KEEP -exec bash -c 'sweep "$0"' {} \; 2>/dev/null