Using Ruby, how can I copy the contents of one directory to another? For example, given (non-empty) directories A and B:
A/
bar
foo
B/
jam
jim
I want to copy everything from A into B, resulting in:
A/
bar
foo
B/
bar
foo
jam
jim
I cannot use FileUtils.cp_r
because it copies the directory itself:
irb(main):001:0> require 'fileutils'
#=> true
irb(main):002:0> Dir['**/*']
#=> ["A", "A/bar", "A/foo", "B", "B/jam", "B/jim"]
irb(main):003:0> FileUtils.cp_r('A','B')
#=> nil
irb(main):004:0> Dir['**/*']
#=> ["A", "A/bar", "A/foo", "B", "B/A", "B/A/bar", "B/A/foo", "B/jam", "B/jim"]
Is there a better (shorter, more efficient) answer than the following?
Dir['A/*'].each{ |f| FileUtils.cp(f,"B") }
question from:
https://stackoverflow.com/questions/11436680/copy-contents-of-one-directory-to-another 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…