From fab45f896cf761ff4dcd0c41a64e863416260c1a Mon Sep 17 00:00:00 2001 From: Greg Anders Date: Fri, 4 Dec 2015 10:30:28 -0600 Subject: [PATCH] Print usage help and add option to hard delete files instead of moving to trash --- README.md | 32 +++++++++++++++++++++++--------- bin/janitor | 11 ++++++++--- install.sh | 19 ++++++++++++++++--- 3 files changed, 47 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 941419c..202244a 100644 --- a/README.md +++ b/README.md @@ -6,16 +6,30 @@ Bash script to automate deleting old files in a specified directory cd Janitor ./install.sh -The install script creates a symlink in your home directory to `janitor.sh` and -appends an entry to the current user's `crontab` (use `crontab -l` to view your active cron jobs) +By default, Janitor will monitor `$HOME/Downloads` and move any file older than +30 days to your Trash folder (`$HOME/.Trash`). These settings can be changed using +the installation script (see below). ## Options -Open the `janitor.sh` file to change options +**Target directory** -| Option | Description | -|:----------------:| ---------------------------------------------------------------------------------------------------------------- | -| **TARGET_DIR** | Define which folder to clean out | -| **DAYS_TO_KEEP** | How long should a file be able to live in the folder? | -| **TRASH_DIR** | Where to move deleted files. Delete this if you want to permanently delete files instead of moving them to Trash | -| **LOG_DIR** | Location to store log files, relative to the Janitor directory | + Specify which directory to monitor + + -d + +**Trash directory** + + Specify where to move files after deletion (if `-x` option is not set) + + -t + +**Number of days to keep files** + + -n + +**Hard delete files** + + Flag indicating to not move files to another directory, but rather permanently delete them + + -x diff --git a/bin/janitor b/bin/janitor index d261925..a8b7578 100755 --- a/bin/janitor +++ b/bin/janitor @@ -1,17 +1,22 @@ #!/usr/bin/env bash # Define which folder to clean out (default: ~/Downloads) -export TARGET_DIR=${1:-$HOME/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:-30} +export DAYS_TO_KEEP=$2 # Trash folder to move deleted files to (default: ~/.Trash) -export TRASH_DIR=${3:-$HOME/.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 diff --git a/install.sh b/install.sh index 33eb946..daf6152 100755 --- a/install.sh +++ b/install.sh @@ -1,10 +1,15 @@ #!/usr/bin/env bash +# Set defaults TARGET_DIR=$HOME/Downloads NUM_DAYS=30 TRASH_DIR=$HOME/.Trash -while getopts "d:t:n:" opt; do +function print_usage() { + echo "Usage: ./install.sh [-x] [-d ] [-t ] [-n ]" +} + +while getopts "xd:t:n:" opt; do case $opt in d) TARGET_DIR="$OPTARG" @@ -12,18 +17,26 @@ while getopts "d:t:n:" opt; do t) TRASH_DIR="$OPTARG" ;; + x) + TRASH_DIR= + ;; n) NUM_DAYS="$OPTARG" ;; \?) - echo "Invalid option: -$OPTARG" >&2 + print_usage + exit 1 ;; esac done echo "Target directory is $TARGET_DIR" echo "Deleting files older than $NUM_DAYS" -echo "Trash direcotry is $TRASH_DIR" +if [ -z "$TRASH_DIR" ]; then + echo "Hard deleting files - not using a Trash directory" +else + echo "Trash direcotry is $TRASH_DIR" +fi function create_crontab() { echo " " -- 2.26.2