How do I copy an entire directory from one place to another?
At the moment I am copying the names of all the sub directories then
creating them in the new location, then recursively going through each
of the sub directories and copying the files to the new directories
created.
This seems like a long way around.
Plus I also want to be able to copy the entire directory to another
machine - I was thinking of opening a socket connection to a listener on
the target machine and trying to copy the files that way.
At the moment I am copying the names of all the sub directories then
creating them in the new location, then recursively going through each
of the sub directories and copying the files to the new directories
created.
Well 1.8 has a module fileutils
···
#
# Options: preserve noop verbose
#
# Copies +src+ to +dest+. If +src+ is a directory, this method copies
# all its contents recursively. If +dest+ is a directory, copies
# +src+ to +dest/src+.
#
# +src+ can be a list of files.
#
# # Installing ruby library "mylib" under the site_ruby
# FileUtils.rm_r site_ruby + '/mylib', :force
# FileUtils.cp_r 'lib/', site_ruby + '/mylib'
#
# # Examples of copying several files to target directory.
# FileUtils.cp_r %w(mail.rb field.rb debug/), site_ruby + '/tmail'
# FileUtils.cp_r Dir.glob('*.rb'), '/home/aamine/lib/ruby', :noop, :verbose
#
def cp_r( src, dest, options = {} )
For copying to the remote machine, I’d say ‘rsync’ is the best tool. It’s
not a Ruby program or library, but you can run it using system() of course.
It runs over ssh.
rsync -a will recursively copy subdirectories and also preserve mtime, mode
bits and ownership (if it is being run with sufficient privileges)
It cleverly optimises bandwidth by working out which files or bits of files
already exist, so only copies what is new.
How do I copy an entire directory from one place to another?
At the moment I am copying the names of all the sub directories then
creating them in the new location, then recursively going through each
of the sub directories and copying the files to the new directories
created.
This seems like a long way around.
Plus I also want to be able to copy the entire directory to another
machine - I was thinking of opening a socket connection to a listener on
the target machine and trying to copy the files that way.
How do I copy an entire directory from one place to another?
At the moment I am copying the names of all the sub directories then
creating them in the new location, then recursively going through each
of the sub directories and copying the files to the new directories
created.
This seems like a long way around.
Plus I also want to be able to copy the entire directory to another
machine - I was thinking of opening a socket connection to a listener on
the target machine and trying to copy the files that way.
Any thoughts?
You can do (all shell level):
local:
cp -a
( cd from && tar cf - . ) | ( cd && tar xf - )
remote
( cd from && tar cf - . ) | ( rsh foo@bar cd ‘&&’ tar xf - )
with GNU tar you can do
( tar cf - -SC . ) | ( rsh foo@bar tar xf - -SC )
and for slow connections even
( tar czf - -SC . ) | ( rsh foo@bar tar xzf - -SC )
For copying to the remote machine, I’d say ‘rsync’ is the best tool. It’s
not a Ruby program or library, but you can run it using system() of
course.
It runs over ssh.
I think Unison is probably a better tool than rsync. It syncs both ways.
Unison uses rsync’s delta algorithm.