Skip to content

Instantly share code, notes, and snippets.

@dmcclory
Created August 23, 2012 21:23
Show Gist options
  • Save dmcclory/3441996 to your computer and use it in GitHub Desktop.
Save dmcclory/3441996 to your computer and use it in GitHub Desktop.
Copying the contents of one directory to another
# you want to recursively copy the contents of one folder to another
# but you don't want to actually copy the first folder into the second
#3 ways to try to copy the contents of one directory to another:
require 'pathname'
require 'fileutils'
include FileUtils
p = Pathname.new('input')
o = Pathname.new('output')
# 1)
p.children.each do |c| # this works
cp_r c, o
end
# 2)
cp_r p.children, o # this works, and is a little simpler than #1
# 3)
cp_r p, o # this doesn't work,
# it copies the input directory, as well as it's contents into output
# result looks like: output/input/CONTENTS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment