copy directories and files recursively using C shell or bash shell

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kumarboston
    New Member
    • Sep 2007
    • 55

    copy directories and files recursively using C shell or bash shell

    Hi all,
    I am trying to copy couple of directories and their files to another place. Each directory have 25-30 files and numbered accordingly, the problem I am facing is, I have to copy files numbered from 4-22 only and leave rest all.
    I have written a code but its only copying one directory and its files and not others.
    Overall /test1/step_1/prodinit_1....3 0 and I want to copy to /test2/step_1/prodinit_4...22
    Code:
    #!/bin/csh -f
    
    set dir1 = /home/rr/copy/test1
    set dir2 = /home/rr/copy/test2
    
    set step = 1
    set stepmax = 30
    set num  = 4
    set nummax = 22
    
        mkdir $dir2/step_${step}
        echo $dir2/step_${step}
    
    while ($step < 30)
       cd $dir2/step_${step}
       cp -r $dir1/step_${step}/prodinit_${num}*.* $dir2/step_${step}
       echo "Copying file .."
       @ num = ${num} + 1
       if ($num == $nummax ) exit
       cd ..
    end
    I am new to shell programming and any help will be appreciated.

    Thanks
    Kumar
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    You can make use of for and while loop for this

    Code:
    DIR="dir1 dir2 dir3 ..."
    dest_dir="directory u need"
    for dir in $DIR
    do
       $i=4
       while [ $i -lt 30 ]
       do
           cp $d/$i".txt" $dest
           $i=`expr $i + 1`
       done
    done
    Start with this pseudo code

    Raghu

    Comment

    Working...