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

Using FileUtils cp_ris usually copying directories, but I cannot exclude the base directory. This is what I wanted to work, but it is not:

FileUtils.cp_r "#{source_path}\\**", target_path, :verbose => true

source_pathhas subdirectories that I want to copy recursively. I just don’t need the actual directory source_path, just everything under it.

I tried to use Dir.glob, but could not figure it out.

This is a copy of Windows, and I know what I can use xcopy, but I want to know how to do it in Ruby.

+4
source share
2 answers

source_path/. source_path/**,

➜  fileutils  ls
cp_files.rb dst         source
➜  fileutils  tree source 
source
β”œβ”€β”€ a.txt
β”œβ”€β”€ b.txt
β”œβ”€β”€ c.txt
└── deep
    └── d.txt

1 directory, 4 files
➜  fileutils  tree dst 
dst

0 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 dst
dst
β”œβ”€β”€ a.txt
β”œβ”€β”€ b.txt
β”œβ”€β”€ c.txt
└── deep
    └── d.txt

1 directory, 4 files

, cp_files.rb :

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

fileutils.copy_entry. , dest. dest, .

.

-1

All Articles