# FileUtils.mv behaving not as documented

**URL:** <https://www.ruby-forum.com/t/fileutils-mv-behaving-not-as-documented/161058>\
**Category:** Ruby\
**Created:** [February 23, 2009, 2:07am UTC](https://www.ruby-forum.com/t/fileutils-mv-behaving-not-as-documented/161058 "2009-02-23T02:07:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/96bed5/32.png) [@alexd](https://www.ruby-forum.com/u/alexd)\
**Post date:** [February 23, 2009, 2:07am UTC](https://www.ruby-forum.com/t/fileutils-mv-behaving-not-as-documented/161058/1 "2009-02-23T02:07:24Z")

</div>

The documentation for FileUtils.mv(src, dest) says that if src and dest  
are on different disk partitions then it is supposed to act as a copy  
instead. But when I use it to copy a file from the “C:” drive to a  
flash drive or mp3 player using another drive letter such as “E:” it  
moves it and doesn’t copy it as advertised. Is this a bug, or is the  
documentation incorrect? I am using Ruby 186-26 on Windows.

-Alex

---

<div class="post-metadata">

**Author:** ![alexd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/96bed5/32.png) [@alexd](https://www.ruby-forum.com/u/alexd)\
**Post date:** [February 23, 2009, 3:29am UTC](https://www.ruby-forum.com/t/fileutils-mv-behaving-not-as-documented/161058/2 "2009-02-23T03:29:29Z")

</div>

On Feb 22, 2009, at 8:06 PM, Alex DeCaria wrote:

> The documentation for FileUtils.mv(src, dest) says that if src and  
> dest  
> are on different disk partitions then it is supposed to act as a copy  
> instead. But when I use it to copy a file from the “C:” drive to a  
> flash drive or mp3 player using another drive letter such as “E:” it  
> moves it and doesn’t copy it as advertised. Is this a bug, or is the  
> documentation incorrect? I am using Ruby 186-26 on Windows.
> 
> -Alex

I think that the difference the documentation is attempting to point  
out is that a mv on a single partition just changes names and pointers  
in directory entries so it makes no difference the size of the file.  
On different partitions, the size of the file will matter because the  
effect is a copy followed by an unlink of the original file.

If you want to copy, call FileUtils.cp  
If you want to move, continue to call FileUtils.mv

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![alexd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/96bed5/32.png) [@alexd](https://www.ruby-forum.com/u/alexd)\
**Post date:** [February 23, 2009, 3:59am UTC](https://www.ruby-forum.com/t/fileutils-mv-behaving-not-as-documented/161058/3 "2009-02-23T03:59:55Z")

</div>

Rob B. wrote:

> On Feb 22, 2009, at 8:06 PM, Alex DeCaria wrote:
> 
> > The documentation for FileUtils.mv(src, dest) says that if src and  
> > dest  
> > are on different disk partitions then it is supposed to act as a copy  
> > instead. But when I use it to copy a file from the “C:” drive to a  
> > flash drive or mp3 player using another drive letter such as “E:” it  
> > moves it and doesn’t copy it as advertised. Is this a bug, or is the  
> > documentation incorrect? I am using Ruby 186-26 on Windows.
> > 
> > -Alex
> 
> I think that the difference the documentation is attempting to point  
> out is that a mv on a single partition just changes names and pointers  
> in directory entries so it makes no difference the size of the file.  
> On different partitions, the size of the file will matter because the  
> effect is a copy followed by an unlink of the original file.
> 
> If you want to copy, call FileUtils.cp  
> If you want to move, continue to call FileUtils.mv
> 
> -Rob
> 
> Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
> removed\_email\_address@domain.invalid

Thanks. Can you explain another curious bit of behavior I’ve noticed?  
I found that if I use the FileUtil.cp(src, dest) to copy a large file  
from say the C: to the E: drive, it takes much longer than if I first  
use FileUtil.cp(src, tmp) to copy the file to a temporary directory on  
the C: drive and then use FileUtil.mv(tmp, dest) to move the file from  
the temporary directory to the E: drive. This has the same result as  
just doing FileUtil.cp(src, dest), but takes much less time. If I’m  
doing this on a lot of files the time saving is significant, but I don’t  
understand why.

In other words, if I do

FileUtil.cp(“C:/somefile”, “C:/Temp/somefile”)  
FileUtil.mv(“C:/Temp/somefile”, “E:/”)

it is faster than if I do

FileUtil.cp(“C:/somefile”, “E:/”)

-Alex

---

<div class="post-metadata">

**Author:** ![alexd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/96bed5/32.png) [@alexd](https://www.ruby-forum.com/u/alexd)\
**Post date:** [February 23, 2009, 5:37am UTC](https://www.ruby-forum.com/t/fileutils-mv-behaving-not-as-documented/161058/4 "2009-02-23T05:37:36Z")

</div>

On Feb 22, 2009, at 9:59 PM, Alex DeCaria wrote:

> > > the
> 
> from say the C: to the E: drive, it takes much longer than if I first  
> FileUtil.cp(“C:/somefile”, “C:/Temp/somefile”)  
> FileUtil.mv(“C:/Temp/somefile”, “E:/”)
> 
> it is faster than if I do
> 
> FileUtil.cp(“C:/somefile”, “E:/”)
> 
> -Alex

I’m making an educated guess (but it is nonetheless speculation) that  
the underlying operating-system-level code that implements mv is using  
a better buffer or at least tighter code which the implementation of  
cp uses some higher-level Ruby code (or a different/smaller buffer).  
It’s also possible that Windows does something quite different between  
cp and mv.

If you’re truly interested, dig into the code and find out the  
differences yourself. (I don’t work on the Windows platform so I have  
no direct access [or interest].)

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
