In C shell How do I copy all files and subdirectories in a directory using foreach loop?

How do I copy all files and subdirectories in a directory using foreach loop?
wademiAsked:
Who is Participating?
 
regevhaCommented:
Foreach usage is:
foreach var ( list )
  statements
end

Open in new window

As you would like to loop over a set of directories, create the directories and copy all files from source to destination directory (i.e. loop over all files in each directory), your script should use nested foreach sections, like:
set cpsource_IPAD = "$SourceDir/ipad"

foreach directory( `ls -d $cpsource_IPAD`)
  mkdir "$DestDir_IPAD/$directory"
  
  foreach file(`find $cpsource_IPAD/$directory -type f`)
    cp $file "$DestDir_IPAD/$directory"
  end

end

Open in new window

0
 
regevhaCommented:
Why not use the cp command in order to recursively copy files and subdirectories ?
cp -R dirA dirB

Open in new window

linux  cp  command
0
 
regevhaCommented:
Another alternative based on foreach will look like:
foreach filename (`ls dirA/`)
    cp dirA/$filename dirB/"$filname".old
end

Open in new window

0
 
wademiAuthor Commented:
Here is a part of my script

set cpsource_IPAD = "$SourceDir/$directory/ipad"

foreach file($cpsource_IPAD/*)
  cp $file "$DestDir_IPAD/$directory"
end

foreach directory($cpsource_IPAD/*)
  cp $directory "$DestDir_IPAD/$directory"
end
0
 
wademiAuthor Commented:
I cant just use       cp -R dirA dirB ebcuase the source and destination folder file structure are different and I want to maintain the structure
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.