Accessing multiple directories in loop

Hi Guys,

I need to access multiple directories whcih is following similar structure and need to copy those files in desitination path.

for eg :

if [[ -e ${DIR}/Mon/loaded ]]
then
cd ${DIR}/Mon/loaded
echo "copying files to $GRS_DIR"
cp * ${DIR}/Mon/
echo "Files of Monday are Copied"
fi

if [[ -e ${DIR}/Tue/loaded ]]
then
cd ${DIR}/Tue/loaded
echo "copying files to $GRS_DIR"
cp * ${DIR}/Mon/
echo "Files of Monday are Copied"
fi

 

like wise i am having common directory like {DIR}/Mon . Tue. Wed. Thu . Fri . Sat will it be possible to achive in single loop

Hello Rohit,

Following may help you, but try the same in non live environment before using this into live environment as this is not tested.

cat test.ksh
check_directories() {
for i in $1
do
        if [[ -e $i ]]
        then
                cd ${DIR}/Mon/loaded
                echo "copying files to $GRS_DIR"
                cp * ${DIR}/Mon/
                echo "Files of Monday are Copied"
        fi
done
}
for i in $DIR/Mon/*
do
        check_directories $i
done

This is a function based script. Please let us know if you have any queries.

Thanks,
R. Singh

Hi Singh,

But this would work in $DIR/Mon/loaded folder alone i need to acces other directoreis as well
$DIR/tue/loaded
$DIR/wed/loaded
.
.

and in your cat test.ksh you want me to add these details?

try

for subdir in mon tue wed thu fri sat
do
if [[ -e ${DIR}/${subdir}/loaded ]]
then
cd ${DIR}/${subdir}/loaded
cp *.* ${DIR}/${subdir}
echo "Files of ${subdir}day are Copied"
cd ${DIR}
fi
done
1 Like

Hello Rohit,

You can use following.

cat test.ksh
check_directories() {
for i in $1
do
        if [[ -e $i ]]
        then
                cd ${DIR}/Mon/loaded
                echo "copying files to $GRS_DIR"
                cp * ${DIR}/Mon/
                echo "Files of Monday are Copied"
        fi
done
}
for i in $DIR/*
do
        check_directories $i
done 

Thanks,
R. Singh

1 Like

How about

for SUBDIR in Mon Tue Wed Thu Fri Sat
do cp -v ${DIR}/$SUBDIR/loaded/* ${DIR]/$SUBDIR
done

and react on the messages?