How can I recursively copy the directory contents and exclude the source directory itself? How can I recursively copy the directory contents and exclude the source directory itself? ruby ruby

How can I recursively copy the directory contents and exclude the source directory itself?


You want to use source_path/. instead of source_path/**, as describe in the last example of the documentation

➜  fileutils  lscp_files.rb dst         source➜  fileutils  tree source source├── a.txt├── b.txt├── c.txt└── deep    └── d.txt1 directory, 4 files➜  fileutils  tree dst dst0 directories, 0 files➜  fileutils  cat cp_files.rb require 'fileutils'FileUtils.cp_r "source/.", 'dst', :verbose => true➜  fileutils  ruby cp_files.rb cp -r source/. dst➜  fileutils  tree dstdst├── a.txt├── b.txt├── c.txt└── deep    └── d.txt1 directory, 4 files

This is what cp_files.rb looks like:

require 'fileutils'FileUtils.cp_r "source/.", 'dst', :verbose => true


Please use the FileUtils.copy_entry utility. Provide the entire path for both source & destination. It will copy recursively from source to destination excluding the source parent directory. This method preserves file types, c.f. symlink, directory… (FIFO, device files and etc. are not supported yet)

Example usage:

src = "/path/to/source/dir"dest = "/path/to/destination/dir"preserve = falsedereference_root = falseremove_destination = falseFileUtils.copy_entry(src, dest, preserve, dereference_root, remove_destination)

Both of src and dest must be a path name. src must exist, dest must not exist.

If preserve is true, this method preserves owner, group, permissions and modified time. Optional use.

If dereference_root is true, this method dereference tree root. Optional use.

If remove_destination is true, this method removes each destination file before copy. Optional use.

For more information, check out the documentation.