[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 568
  • Last Modified:

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?
0
wademi
Asked:
wademi
  • 3
  • 2
1 Solution
 
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
 
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

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now