Recursion to Copy a Directory structure


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Recursion to Copy a Directory structure
# 1  
Old 03-28-2008
Recursion to Copy a Directory structure

I have the below code to recurse through a given file path. I am able to go to the leaf level once recursively but traversing out is not possible as the required varaibles don't have the values on return. Both typeset and local don't work for my variable definitions. I have the o/p of the sample run attached as well with some debug statements. Pls. help me get out of this. Thanks a ton in advance.

===============
CODE
===============

CreateElement()
{
srcDir=$1
destDir=$2
lastKnownDir="$3"

cd $srcDir
depth=`ls | cut -f1 | wc -l`
echo "Inside CreateElement with Depth -> $depth \n"

j=1
while [ $j -le $depth ]
do
a=`ls | cut -f1 | head -$j | tail -1`
echo "0-> $a is the file/dir being processed and $j -> is j value\n"
isdir=`ls -p | cut -f1 | head -$j | tail -1 | grep /`
#echo "1-> isdir = $isdir ,\n"
if [ "$isdir" != "" ]; then
cd $destDir
if [ -d $a ]; then
echo "\nDirecotry $a already exists!\n"
else
#cc_mkdir $a
mkdir "$a"
fi
srcDir=$srcDir/$isdir
destDir=$destDir/$isdir
lastKnownDir="$isdir"
echo "1 -> lastKnownDir = $lastKnownDir\n"
CreateElement $srcDir $destDir $lastKnownDir
else
cd $destDir
echo "2 -> destDir is $destDir in the file handling code\n"
echo "3 -> srcDir is $srcDir in the file handling code\n"
echo "4 -> lastKnownDir is $lastKnownDir in FHC\n"
if [ -f $a ]; then
echo "\nFile $a already exists!\n"
else
#cc_mkelem $a
echo "create element -> $a \n"
fi
cp $srcDir$a .
fi
j=`expr $j + 1`
echo "5 -> J value at end of loop is $j, before Done\n"
done

if [ $j -gt $depth ]; then
echo "$j == $depth moving one directory up !!!!!\n"
echo "lastknownDir at end is -> $lastKnownDir *****\n"
srcDir=`echo $srcDir | sed -e "s/"$lastKnownDir"//"`
destDir=`echo $destDir | sed -e "s/$lastKnownDir//"`
fi

echo "isdir is -> $isdir \n"
echo "\nsrcDir -> $srcDir and destDir -> $destDir \n"

echo "Just before return from CreateElement\n"
return
}


#DESTN="$IPCBU_AUTO_HOME/scripts/"
DESTN="/users/cpati/"

clear

### Check for number of arguments ###
if [ $# -ne 1 ]; then
echo "Usage : $0 <Absolute Path of source directory>\n"
exit
fi

SRC=$1

### Check whether Source path has a / at the end ###
slash=`echo $1 | grep /$`
if [ "$slash" = "" ]; then
SRC=$SRC"/"
fi

### First level ###
cd $SRC
count=`ls | cut -f1 | wc -l`

## Check for existance of files and dir ##
if [ $count -eq 0 ]; then
echo "WARNING!!There are no files in the specified source to check in\n"
exit
fi

## Check for change in destn directory ##
echo "\n"
echo "The default directory to check in your feature code is $DESTN"
echo "Do you wish to change the destination(Y/N)?\c"
read option
if [ "$option" = "Y" ]; then
echo "Please specify the absolute path of the new destination."
read DESTN
fi

### Check whether Destination path has a / at the end ###
slashd=`echo $DESTN | grep /$`
if [ "$slashd" = "" ]; then
DESTN=$DESTN"/"
fi

## Get input from user ##
echo "\n"
echo "Please give a folder name(preferrably your feature) to create under $DESTN"
read feature

cd $DESTN
if [ -d $feature ]; then
echo "\n"
echo "The folder $feature already exists under $DESTN! Do you still want to continue(Y/N)?\c"
read answer
if [ "$answer" != "Y" ]; then
exit
fi
else
echo "\n"
echo "Element creation begins........."
echo "\n"
#cc_mkdir $feature
mkdir $feature
fi

i=1
srcDir=$1
destDir=$DESTN$feature

while [ $i -le $count ]
do

echo "calling CreateElement for first time\n"
CreateElement $srcDir $destDir ""
i=`expr $i+1`

done


echo "\n"
echo "All the Directory and File elements have been successfully created!"
echo "\n"
echo "Please use cc_update -m LATEST to continue with the check-in process......"
echo "\n"

=============================================

Pls. ignore the commented code.

==============
Result
==============


bash-2.05b$ ./ModifiedACT.txt /users/cpati/srcDir/


The default directory to check in your feature code is /users/cpati/
Do you wish to change the destination(Y/N)?N


Please give a folder name(preferrably your feature) to create under /users/cpati/
Ravi


Element creation begins.........


calling CreateElement for first time

Inside CreateElement with Depth -> 3

0-> Dir1_test is the file/dir being processed and 1 -> is j value

1 -> lastKnownDir = Dir1_test/

Inside CreateElement with Depth -> 3

0-> Dir12 is the file/dir being processed and 1 -> is j value

1 -> lastKnownDir = Dir12/

Inside CreateElement with Depth -> 2

0-> file112.cti is the file/dir being processed and 1 -> is j value

2 -> destDir is /users/cpati/Ravi/Dir1_test//Dir12/ in the file handling code

3 -> srcDir is /users/cpati/srcDir//Dir1_test//Dir12/ in the file handling code

4 -> lastKnownDir is Dir12/ in FHC

create element -> file112.cti

5 -> J value at end of loop is 2, before Done

0-> file112.cti is the file/dir being processed and 2 -> is j value

2 -> destDir is /users/cpati/Ravi/Dir1_test//Dir12/ in the file handling code

3 -> srcDir is /users/cpati/srcDir//Dir1_test//Dir12/ in the file handling code

4 -> lastKnownDir is Dir12/ in FHC


File file112.cti already exists!

5 -> J value at end of loop is 3, before Done

3 == 2 moving one directory up !!!!!

lastknownDir at end is -> Dir12/ *****

sed: command garbled: s/Dir12///
sed: command garbled: s/Dir12///
isdir is ->


srcDir -> and destDir ->

Just before return from CreateElement

5 -> J value at end of loop is 4, before Done

4 == 2 moving one directory up !!!!!

lastknownDir at end is -> Dir12/ *****

sed: command garbled: s/Dir12///
sed: command garbled: s/Dir12///
isdir is ->


srcDir -> and destDir ->

Just before return from CreateElement

5 -> J value at end of loop is 5, before Done

5 == 2 moving one directory up !!!!!

lastknownDir at end is -> Dir12/ *****

sed: command garbled: s/Dir12///
sed: command garbled: s/Dir12///
isdir is ->


srcDir -> and destDir ->

Just before return from CreateElement

calling CreateElement for first time

Inside CreateElement with Depth -> 19

0-> 142_unison is the file/dir being processed and 1 -> is j value


Direcotry 142_unison already exists!

1 -> lastKnownDir = 142_unison/

./ModifiedACT.txt: /142_unison/: does not exist


===============================================
# 2  
Old 03-28-2008
Your code shows that you know VERY LITTLE about the UNIX-Commands as well as scripting!!

Even if you complete this ASSIGNMENT (and the course for which this is), it won't provide you with any benefits UNLESS you learn the UNIX-Commands very well.

Nevertheless, I've corrected your code as little as to show you the "problem areas" and they are marked with #AAAAAAAA for added-code and #CCCCCC for changed-code. I hope you see why it wasn't working!?!?

Code:
CreateElement() {
    srcDir=$1
    destDir=$2
    lastKnownDir="$3"

    cd $srcDir
    depth=`ls | cut -f1 | wc -l`
    echo "Inside CreateElement with Depth -> $depth \n"

    j=1
    while [ $j -le $depth ]
    do
        a=`ls | cut -f1 | head -$j | tail -1`
        echo "0-> $a is the file/dir being processed and $j -> is j value\n"
        isdir=`ls -p | cut -f1 | head -$j | tail -1 | grep /`
        #echo "1-> isdir = $isdir ,\n"
        if [ "$isdir" != "" ]; then
            cd $destDir
            if [ -d $a ]; then
                echo "\nDirecotry $a already exists!\n"
            else
            #cc_mkdir $a
                mkdir "$a"
            fi
            srcDir=$srcDir$a        #CCCCCCCCCCCC   $srcDir/$isdir
            destDir=$destDir$a      #CCCCCCCCCCCC   $destDir/$isdir
            lastKnownDir="$isdir"
            echo "1 -> lastKnownDir = $lastKnownDir\n"
            CreateElement $srcDir $destDir $lastKnownDir
        else
            cd $destDir
            echo "2 -> destDir is $destDir in the file handling code\n"
            echo "3 -> srcDir is $srcDir in the file handling code\n"
            echo "4 -> lastKnownDir is $lastKnownDir in FHC\n"
            if [ -f $a ]; then
                echo "\nFile $a already exists!\n"
            else
                #cc_mkelem $a
                echo "create element -> $a \n"
            fi
            cp $srcDir$a .
        fi
        j=`expr $j + 1`
        echo "5 -> J value at end of loop is $j, before Done\n"
    done

    if [ $j -gt $depth ]; then
        echo "$j == $depth moving one directory up !!!!!\n"
        echo "lastknownDir at end is -> $lastKnownDir *****\n"
        srcDir=`echo $srcDir | sed "s=$lastKnownDir=="`       #CCCCCCCCCCCCC  -e "s/"$lastKnownDir"//"`
        destDir=`echo $destDir | sed "s=$lastKnownDir=="`     #CCCCCCCCCCCCC  -e "s/$lastKnownDir//"`
    fi

    echo "isdir is -> $isdir \n"
    echo "\nsrcDir -> $srcDir and destDir -> $destDir \n"

    echo "Just before return from CreateElement\n"
    return
}


#DESTN="$IPCBU_AUTO_HOME/scripts/"
DESTN="/users/cpati/"

clear

### Check for number of arguments ###
if [ $# -ne 1 ]; then
    echo "Usage : $0 <Absolute Path of source directory>\n"
    exit
fi

SRC=$1

### Check whether Source path has a / at the end ###
slash=`echo $1 | grep /$`
if [ "$slash" = "" ]; then
    SRC=$SRC"/"
fi

### First level ###
cd $SRC
count=`ls | cut -f1 | wc -l`

## Check for existance of files and dir ##
if [ $count -eq 0 ]; then
    echo "WARNING!!There are no files in the specified source to check in\n"
    exit
fi

## Check for change in destn directory ##
echo "\n"
echo "The default directory to check in your feature code is $DESTN"
echo "Do you wish to change the destination(Y/N)?\c"
read option
if [ "$option" = "Y" ]; then
    echo "Please specify the absolute path of the new destination."
    read DESTN
fi

### Check whether Destination path has a / at the end ###
slashd=`echo $DESTN | grep /$`
if [ "$slashd" = "" ]; then
    DESTN=$DESTN"/"
fi

## Get input from user ##
echo "\n"
echo "Please give a folder name(preferrably your feature) to create under $DESTN"
read feature
#AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
### Check whether feature has a / at the end ###
slashd=`echo $feature | grep /$`
if [ "$slashd" = "" ]; then
    feature=$feature"/"
fi
#AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

cd $DESTN
if [ -d $feature ]; then
    echo "\n"
    echo "The folder $feature already exists under $DESTN! Do you still want to continue(Y/N)?\c"
    read answer
    if [ "$answer" != "Y" ]; then
        exit
    fi
else
    echo "\n"
    echo "Element creation begins........."
    echo "\n"
    #cc_mkdir $feature
    mkdir $feature
fi

i=1
srcDir=$SRC                  #CCCCCCCCCCCCCCC $1
destDir=$DESTN$feature

while [ $i -le $count ]
do
    echo "calling CreateElement for first time\n"
    CreateElement $srcDir $destDir ""
    i=`expr $i+1`
done


echo "\n"
echo "All the Directory and File elements have been successfully created!"
echo "\n"
echo "Please use cc_update -m LATEST to continue with the check-in process......"
echo "\n"

# 3  
Old 03-31-2008
Recursion to Copy a Directory structure

Thanks for your inputs. Yes, am new to shell scripting. The srcDir and destDir variables are working fine now. But still the recursion control variable J is behaving like a Global variable. How do i make "J" as a local variable so that there are copies of the variable J created in every call to createelement? Pls. let me know. Tried "typeset j=0" as the first line of the file but doesn't seem to work.

Last edited by avrkiran; 03-31-2008 at 03:58 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Copy directory withOUT recursion

Hi, I cannot find a way to copy a directory to another location with all attributes (mode, ownership, timestamps) but withOUT recursion (after so many years of working with Linux). Say I want to create /home/jail/tmp exactly like /tmp but with nothing in it. Here is what I tried: ... (7 Replies)
Discussion started by: chebarbudo
7 Replies

2. Shell Programming and Scripting

Directory Tree/Recursion Issue . . .

Hope the title is close enough; didn't quite know how to put this... OK. We have a hypothetical script, script.a, with one simplistic line:bash ./nextDir/script.bNow script.b has a bit more going for it; and does file moves, copies, and renames, calling all the targets with respect to itself.... (7 Replies)
Discussion started by: LinQ
7 Replies

3. Programming

Deep copy of structure in C

Hi , I have a scenario where i need to copy the iter to another local variable , where iter is of type MCC_T_SYS_ADDRINFO *iter . struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; ... (5 Replies)
Discussion started by: breezevinay
5 Replies

4. Shell Programming and Scripting

Need to copy a directory structure from one server to another

Hello All, I have got a dev server and a production server will call it as D server and P server. I have a dir structure in my D server and i need to create the same dir structure in P server also using a shell script. If i do a find . in my D server, i am getting an o/p like :- . ./vio... (9 Replies)
Discussion started by: Renjesh
9 Replies

5. Shell Programming and Scripting

copy directory structure to a system on the network

I am trying to write a script which has to copy the directory structure from my system to another system on the network. But I dont want the files to be copied. I think I have to start with copying all subdirectories names in a directory to a system on the network. Here's the case: Source... (1 Reply)
Discussion started by: firefox211
1 Replies

6. UNIX for Dummies Questions & Answers

copy files with directory structure

i have a text file as. /database/sp/NTR_Update_Imsi_List.sql /database/sp/NTR_Update_Imsi_Range_List.sql /database/sp/NTR_Vlr_Upload.sql /database/tables/StatsTables.sql /mib/ntr.mib /mib/ntr.v2.mib /scripts/operations/ntr/IMSITracer.ph /scripts/operations/ntr/IMSITracer.pl ... (3 Replies)
Discussion started by: adddy
3 Replies

7. UNIX for Advanced & Expert Users

MV files from one directory structure(multiple level) to other directory structure

Hi, I am trying to write a script that will move all the files from source directory structure(multiple levels might exist) to destination directory structure. If a sub folder is source doesnot exist in destination then I have to skip and goto next level. I also need to delete the files in... (4 Replies)
Discussion started by: srmadab
4 Replies

8. UNIX for Dummies Questions & Answers

Copying a Directory Structure to a new structure

Hi all Is it possible to copy a structure of a directory only. e.g. I have a file with the following entries that is a result of a find :- /dir1/dir2/file.dbf /dir1/dir2/dir3/file1.dbf /dir1/file.dbf I want to copy these to a directory and keep the structure however starting at a new dir... (8 Replies)
Discussion started by: jhansrod
8 Replies

9. UNIX for Dummies Questions & Answers

any idea to copy directory structure

I want to copy the durectory structure having subdirectories in it ... would be greatful if anyone could let me know .. thanks in advance (6 Replies)
Discussion started by: myelvis
6 Replies
Login or Register to Ask a Question