# Moving files with ruby

**URL:** <https://www.ruby-forum.com/t/moving-files-with-ruby/66216>\
**Category:** Ruby\
**Created:** [August 24, 2006, 1:26am UTC](https://www.ruby-forum.com/t/moving-files-with-ruby/66216 "2006-08-24T01:26:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![darren\_kirby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed8c4c/32.png) [@darren\_kirby](https://www.ruby-forum.com/u/darren_kirby)\
**Post date:** [August 24, 2006, 1:26am UTC](https://www.ruby-forum.com/t/moving-files-with-ruby/66216/1 "2006-08-24T01:26:47Z")

</div>

Hello all,

A script I have written needs the ability to move files. I thought I  
would be  
clever and use File.rename(old, newdir + new) but this is giving me an  
error:  
`rename’: Invalid cross-device link. I gather this is because I am  
renaming  
the file to a different partition.

So then I found FileUtils and used:  
require ‘fileutils’  
include FileUtils  
mv( old, new)

This is working but seems an overly verbose and convoluted way to move a  
file.  
I had a look at the fileutils.rb source and found this (for ‘mv’):

# Moves file(s) +src+ to +dest+. If +file+ and +dest+ exist on the

different

# disk partition, the file is copied instead

So I guess my real question is: is there a technical reason or some  
arbitrary  
reason there is no File.move() method that can handle this  
transparently? I  
would think it would be quite useful…

Any insight is appreciated,  
-d

---

<div class="post-metadata">

**Author:** ![darren\_kirby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed8c4c/32.png) [@darren\_kirby](https://www.ruby-forum.com/u/darren_kirby)\
**Post date:** [August 24, 2006, 1:46am UTC](https://www.ruby-forum.com/t/moving-files-with-ruby/66216/2 "2006-08-24T01:46:49Z")

</div>

darren kirby wrote:

> Hello all,
> 
> A script I have written needs the ability to move files. I thought I would  
> be clever and use File.rename(old, newdir + new) but this is giving me an  
> error: `rename’: Invalid cross-device link. I gather this is because I am  
> renaming the file to a different partition.

No, it is because you are trying to use “rename” to physically move a  
file,  
and the system is objecting to this misconception. File.rename can only  
rename files, it cannot move them. It works only on one storage  
device/volume/whatever.

> So then I found FileUtils and used:  
> require ‘fileutils’  
> include FileUtils  
> mv( old, new)
> 
> This is working but seems an overly verbose and convoluted way to move a  
> file.

Umm, compared to what? It moves the file using a method named “mv”, just  
like the Linux/Unix command of the same name.

> I had a look at the fileutils.rb source and found this (for ‘mv’):
> 
> # Moves file(s) +src+ to +dest+. If +file+ and +dest+ exist on the
> 
> # different disk partition, the file is copied instead

That is what “mv” does. If the new and old filenames and paths allow the  
file to remain on the same device, a simple rename is carried out. If  
not,  
the file is copied to the destination and deleted from the source.

> So I guess my real question is: is there a technical reason or some  
> arbitrary reason there is no File.move() method that can handle this  
> transparently? I would think it would be quite useful…

It might be because not all operating systems on which Ruby can be  
installed  
know the “mv” operation as described above. Just a guess.

---

<div class="post-metadata">

**Author:** ![darren\_kirby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed8c4c/32.png) [@darren\_kirby](https://www.ruby-forum.com/u/darren_kirby)\
**Post date:** [August 24, 2006, 2:10am UTC](https://www.ruby-forum.com/t/moving-files-with-ruby/66216/3 "2006-08-24T02:10:50Z")

</div>

quoth the Paul L.:

> rename files, it cannot move them. It works only on one storage  
> device/volume/whatever.

Well, I think that’s pretty much what I said. If you ‘rename’ a file  
from /somepath/somefile.txt to /someotherpath/somefile.txt within the  
same  
partition you have effectively ‘moved’ it haven’t you…

> > So then I found FileUtils and used:  
> > require ‘fileutils’  
> > include FileUtils  
> > mv( old, new)
> > 
> > This is working but seems an overly verbose and convoluted way to move a  
> > file.
> 
> Umm, compared to what? It moves the file using a method named “mv”, just  
> like the Linux/Unix command of the same name.

I am typically very impressed with how much you can do with a little  
amount of  
code in Ruby. For instance, I only had to add one line to add some  
recursive  
directory handling in my script:

From:  
files += Dir.glob("_.#{t}")  
To:  
files += Dir.glob("\*\*/_.#{t}")

Whereas to move a file, a fairly basic operation, I need four lines of  
code:

require ‘fileutils’  
include FileUtils  
cp(old, new )  
rm (old)

I have to change “mv” to “cp” and add the “rm” because the old file is  
not  
automatically removed…

> > I had a look at the fileutils.rb source and found this (for ‘mv’):
> > 
> > # Moves file(s) +src+ to +dest+. If +file+ and +dest+ exist on the
> > 
> > # different disk partition, the file is copied instead
> 
> That is what “mv” does. If the new and old filenames and paths allow the  
> file to remain on the same device, a simple rename is carried out. If not,  
> the file is copied to the destination and deleted from the source.

Right, I’ve got that…

> > So I guess my real question is: is there a technical reason or some  
> > arbitrary reason there is no File.move() method that can handle this  
> > transparently? I would think it would be quite useful…
> 
> It might be because not all operating systems on which Ruby can be  
> installed know the “mv” operation as described above. Just a guess.

I am just wondering because there are (AFAIK) other methods that handle  
the  
operation transparently even though the underlying OS does it  
differently.

Not trying to troll, I am just curious…

-d

---

<div class="post-metadata">

**Author:** ![darren\_kirby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed8c4c/32.png) [@darren\_kirby](https://www.ruby-forum.com/u/darren_kirby)\
**Post date:** [August 24, 2006, 5:31am UTC](https://www.ruby-forum.com/t/moving-files-with-ruby/66216/4 "2006-08-24T05:31:05Z")

</div>

Marc H. wrote:

> well  
> system(“mv #{file}”)  
> is also possible ,)

But not generic for cross-platform re-use.

---

<div class="post-metadata">

**Author:** ![darren\_kirby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed8c4c/32.png) [@darren\_kirby](https://www.ruby-forum.com/u/darren_kirby)\
**Post date:** [August 24, 2006, 4:13am UTC](https://www.ruby-forum.com/t/moving-files-with-ruby/66216/5 "2006-08-24T04:13:12Z")

</div>

well  
system(“mv #{file}”)  
is also possible ,)

include FileUtils isnt needed so that should reduce your  
LOC by -1

Doesnt FileUtils have  
mv(src, dest, options)  
?

Anyway, why not this?

[http://www.ruby-doc.org/core/classes/FileUtils.html#M002613](http://www.ruby-doc.org/core/classes/FileUtils.html#M002613)

---

<div class="post-metadata">

**Author:** ![darren\_kirby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed8c4c/32.png) [@darren\_kirby](https://www.ruby-forum.com/u/darren_kirby)\
**Post date:** [August 24, 2006, 6:01am UTC](https://www.ruby-forum.com/t/moving-files-with-ruby/66216/6 "2006-08-24T06:01:08Z")

</div>

quoth the Marc H.:

> well  
> system(“mv #{file}”)  
> is also possible ,)

I may be wrong, but I always had the impression that using a system  
command to  
do something available within the language is considered a kludge.

> include FileUtils isnt needed so that should reduce your  
> LOC by -1

When I comment the line out I get:  
undefined method `cp’ for main:Object (NoMethodError)

> Doesnt FileUtils have  
> mv(src, dest, options)  
> ?

Yes, it does. But the thing is that if you are moving the file across  
partitions it will only copy and not delete the original file. I will  
not  
know if we are moving cross-partition, so I have to use cp and rm…

> Anyway, why not this?
> 
> [http://www.ruby-doc.org/core/classes/FileUtils.html#M002613](http://www.ruby-doc.org/core/classes/FileUtils.html#M002613)

From the link: “If file and dest exist on the different disk partition,  
the  
file is copied instead.”

-d
