Recursion in Bash-Shell Script

1155

#!/bin/bash

## ———-
## This script will move all the TEXT files with name containing numeric only to path “/dump”.
## If filename already exist at destination path, it will not move the file then.
##
## Log will be maintained containing path along with filename.
## ———-

VAR_BASE_PATH=/data
VAR_DUMP_PATH=/dump

LOG_NAME=$(date +”recursion_%d%m%Y_%H%M.log”)

## ———-
## modify IF statement if
## source and destination path need to pass through arguments
## ———-

if [ $# -ge 1 ];then
    echo “wrong input, do not pass any argument”
    exit 1
fi

function SCAN_DIR ()
{
    ls “${1}” | while read line
    do
        new_path=”${1}/${line}”

        if [ -d “$new_path” ];then
            SCAN_DIR “${new_path}”
        else
            echo “$line” | grep -qE “^[0-9]+[0-9].txt”
            if [ $? -eq 0 ];then
                echo “$new_path” >> ${LOG_NAME}
                mv -n “$new_path” ${VAR_DUMP_PATH}/
            fi
        fi
#        sleep 1
    done
##    echo “exiting function SCAN_DIR”
}

SCAN_DIR “${VAR_BASE_PATH}”

exit 0

## —- END —- ##