diff --git a/conf/delete_all_from_dirs.conf.sample b/conf/delete_all_from_dirs.conf.sample new file mode 100644 index 0000000..a2879bb --- /dev/null +++ b/conf/delete_all_from_dirs.conf.sample @@ -0,0 +1,22 @@ +# ------------------------------------ +# - Settings for script delete_all_from_dirsh.conf +# ------------------------------------ + +# - days +# - +# - Files older then 'days' will be deleted. +# - +# - days=0 means: delete all files/directories +# - +# - Defaults to: days=1 +# - +#days=1 + +# - dirs_to_clean_up +# - +# - Directories where files older than given days will be deleted. +# - +# - Example: +# - dirs_to_clean_up=/data/samba/scan /data/samba/temp +# - +#dirs_to_clean_up="" diff --git a/delete_all_from_dirs.sh b/delete_all_from_dirs.sh new file mode 100755 index 0000000..68cb7c3 --- /dev/null +++ b/delete_all_from_dirs.sh @@ -0,0 +1,212 @@ +#!/usr/bin/env bash + +script_name="$(basename $(realpath $0))" +working_dir="$(dirname $(realpath $0))" +conf_file="${working_dir}/conf/${script_name%%.*}.conf" + +LOCK_DIR="/tmp/${script_name%%.*}.LOCK" + + +# ------------- +# - Some functions +# ------------- + +clean_up() { + + # Perform program exit housekeeping + rm -rf "$LOCK_DIR" + blank_line + exit $1 +} + +echononl(){ + if $terminal ; then + echo X\\c > /tmp/shprompt$$ + if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then + echo -e -n "$*\\c" 1>&2 + else + echo -e -n "$*" 1>&2 + fi + rm /tmp/shprompt$$ + fi +} + +fatal(){ + echo "" + if $terminal ; then + echo -e " [ \033[31m\033[1mFatal\033[m ]: $*" + echo "" + echo -e " \033[31m\033[1mScript was terminated\033[m!" + else + echo " [ Fatal ]: $*" + echo "" + echo " Script was terminated...." + fi + echo "" + clean_up 1 +} + +error (){ + echo "" + if $terminal ; then + echo -e " [ \033[31m\033[1mError\033[m ]: $*" + else + echo "[ Error ]: $*" + fi + echo "" +} + +warn (){ + echo "" + if $terminal ; then + echo -e " [ \033[33m\033[1mWarning\033[m ]: $*" + else + echo "[ Warning ]: $*" + fi + echo "" +} +info (){ + + if $terminal ; then + echo "" + echo -e " [ \033[32m\033[1mInfo\033[m ]: $*" + echo "" + fi +} + +echo_done() { + if $terminal ; then + echo -e "\033[85G[ \033[32mdone\033[m ]" + fi +} +echo_failed(){ + if $terminal && $LOGGING ; then + echo -e "\033[85G[ \033[1;31mfailed\033[m ]" + fi +} +echo_skipped() { + if $terminal && $LOGGING ; then + echo -e "\033[85G[ \033[90mskipped\033[m ]" + fi +} +echo_wait(){ + if $terminal ; then + echo -en "\033[85G[ \033[5m\033[1m...\033[m ]" + fi +} + +trim() { + local var="$*" + var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters + var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters + echo -n "$var" +} +blank_line() { + if $terminal ; then + echo "" + fi +} + + +# ------------- +# - Running in a terminal? +# ------------- + +if [[ -t 1 ]] ; then + terminal=true +else + terminal=false +fi + + +# ------------- +# - Read Configurations from $conf_file +# ------------- + +if [[ ! -f "$conf_file" ]]; then + fatal " Configuration file '$(basename ${conf_file})' not found!" +else + source "$conf_file" +fi + +[[ -z "$(trim $dirs_to_clean_up)" ]] && fatal "No directory given!" +[[ -z "$days" ]] && days=1 + + +# ------------- +# - Job is already running? +# ------------- + +# - If job already runs, stop execution.. +# - +if mkdir "$LOCK_DIR" 2> /dev/null ; then + + # - Remove lockdir when the script finishes, or when it receives a signal + # - + trap clean_up SIGHUP SIGINT SIGTERM + +else + + datum="$(date +"%d.%m.%Y %H:%M")" + + msg=" [ Error ]: A previos instance of '$(basename $0)' seems already be running.\n\n Exiting now.." + + error "A previos instance of the script '$(basename $0)' seems already be running." + + exit 1 + +fi + + +traverse() { + + while IFS='' read -r -d '' name ; do + + [[ "$name" = ".." ]] && continue + [[ "$name" = "." ]] && continue + traverse "$name" + + done < <(find "$1" -mindepth 1 -maxdepth 1 -type d -print0) + + if [ ${days} == 0 ]; then + find "$1" -mindepth 1 -exec rm -f "{}" \; > /dev/null 2>&1 + else + find "$1" -mindepth 1 -maxdepth 1 -type f -mtime +${days} -exec rm -f "{}" \; > /dev/null 2>&1 + fi + +} + +blank_line + +for dir in $dirs_to_clean_up ; do + #info "Delete files from trash directory \033[1m${dir}\033[m older than \033[1m${days}\033[m days .." + echononl " Delete files from directory \033[1m${dir}\033[m older than \033[1m${days}\033[m days .." + if [[ -d "$dir" ]]; then + echo_wait + traverse "$dir" + echo_done + else + echo_skipped + fi +done + +# - Delete empty sub-directories +# - +if $terminal ; then + echo "" + echo -e "\033[32m\033[1m-----\033[m" + echo "" +fi +for dir in $dirs_to_clean_up ; do + #info "Cleanup trash directory \033[1m${dir}\033[m from empty directories .." + echononl " Cleanup directory \033[1m${dir}\033[m from empty directories .." + if [[ -d "$dir" ]]; then + echo_wait + find $dir -depth -mindepth 1 -type d -empty -delete + echo_done + else + echo_skipped + fi +done + +clean_up 0