Linux.com

Home Linux Community Community Blogs General Linux Recursion in Bash-Shell Script

Recursion in Bash-Shell Script



#!/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 ---- ##

 

Comments

Subscribe to Comments Feed

Upcoming Linux Foundation Courses

  1. LFS230 Linux Network Management
    06 Oct » 09 Oct - Virtual
    Details
  2. LFD331 Developing Linux Device Drivers
    13 Oct » 17 Oct - Virtual
    Details
  3. LFS430 Linux Enterprise Automation
    13 Oct » 16 Oct - Virtual
    Details

View All Upcoming Courses


Who we are ?

The Linux Foundation is a non-profit consortium dedicated to the growth of Linux.

More About the foundation...

Frequent Questions

Join / Linux Training / Board