# FileUtils.cp\_r - works with regex?

**URL:** <https://www.ruby-forum.com/t/fileutils-cp-r-works-with-regex/159823>\
**Category:** Ruby\
**Created:** [February 9, 2009, 8:51am UTC](https://www.ruby-forum.com/t/fileutils-cp-r-works-with-regex/159823 "2009-02-09T08:51:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![edouard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c57346/32.png) [@edouard](https://www.ruby-forum.com/u/edouard)\
**Post date:** [February 9, 2009, 8:51am UTC](https://www.ruby-forum.com/t/fileutils-cp-r-works-with-regex/159823/1 "2009-02-09T08:51:19Z")

</div>

Hi,

I try to copy files that match a certain pattern to another dir, i write

cp\_r //downloads/bsd/i, “~/biblio/bsd/”

if i can use regex to do this what is wrong in my approach?

thanks for comments

---

<div class="post-metadata">

**Author:** ![edouard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c57346/32.png) [@edouard](https://www.ruby-forum.com/u/edouard)\
**Post date:** [February 9, 2009, 11:18am UTC](https://www.ruby-forum.com/t/fileutils-cp-r-works-with-regex/159823/2 "2009-02-09T11:18:46Z")

</div>

Edouard D. wrote:

> Hi,
> 
> I try to copy files that match a certain pattern to another dir, i write
> 
> cp\_r //downloads/bsd/i, “~/biblio/bsd/”
> 
> if i can use regex to do this

There’s nothing in the standard library docs that suggests you can use a  
regex for the src argument. The description of cp\_r says:

* * *

If src is a directory, this method copies all its contents  
recursively…

## src can be a list of files.

It doesn’t say src can be a regex and that ruby will search your entire  
hard drive for matching directories.

> what is wrong in my approach?

I can’t get cp\_r to work when providing multiple directories as the  
source, for example:

cp\_r(%w{./dir1 ./dir2}, “./test\_dir”)

produces this error:

/usr/lib/ruby/1.8/fileutils.rb:475:in `mkdir’: No such file or directory

- /blah/blah/dir1/test\_dir/dir1 (Errno::ENOENT)

where dir1 is the current working directory.

A solution would be to use Dir.glob() to search your hard drive for  
matching directories, for instance:

arr = Dir.glob("/downloads/bsd", File::FNM\_CASEFOLD)

and then step through the array using each(), and then call cp\_r() on  
each directory.

---

<div class="post-metadata">

**Author:** ![edouard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c57346/32.png) [@edouard](https://www.ruby-forum.com/u/edouard)\
**Post date:** [February 9, 2009, 11:52am UTC](https://www.ruby-forum.com/t/fileutils-cp-r-works-with-regex/159823/3 "2009-02-09T11:52:23Z")

</div>

7stud – wrote:

> Edouard D. wrote:
> 
> > Hi,
> > 
> > I try to copy files that match a certain pattern to another dir, i write
> > 
> > cp\_r //downloads/bsd/i, “~/biblio/bsd/”
> > 
> > if i can use regex to do this

I did some more testing and FileUtils seems pretty buggy to me. For  
instance, if my ruby program is in this directory:

/Users/me/2testing/dir1

and I execute these two commands:

cp\_r ("/Users/me/downloads/bsd", “./test\_dir”)  
cp\_r("/Users/me/2testing/dir1/downloads/bsd", “./test\_dir”)

and the respective directories contain these files:

/Users/me/downLOads/bSd/test1.txt  
/Users/me/2testing/dir1/DownloadS/BSd/test2.txt

Then I get these results:

…dir1  
…test\_dir  
…test2.txt  
…bsd  
…test1.txt

## According to the FileUtils docs:

## If src is a directory, this method copies all its contents recursively. If dest is a directory, copies src to +dest/src+.

So I would have expected test1.txt to be copied to the directory:

/Users/me/2testing/dir1/test\_dir/Users/me/downloads/bsd

And test2.txt to be copied to the directory:

/Users/me/2testing/dir1/test\_dir/Users/me/2testing/dir1/downloads/bsd
