tags:

views:

5114

answers:

3

I want to move a file with ruby. How do I do that?

+1  A: 

FileUtils.move

require "FileUtils"
FileUtils.move 'stuff.rb', '/notexist/lib/ruby'
Željko Filipin
+1 Note that .move is an alias of .mv so you should be able to do FileUtils.mv too.
Tim K.
Thanks, I just like .move more that .mv. :)
Željko Filipin
I don't. `mv` makes me feel like I'm in my beloved console ;)
Erik Escobedo
And the nice thing about Ruby is that it has both `move` and `mv`so one can pick either one. :)
Željko Filipin
+1  A: 

Use the module 'fileutils' and use FileUtils.mv:

http://www.ruby-doc.org/core/classes/FileUtils.html#M004361

Nicolas Martyanoff
+14  A: 

You can use FileUtils to do this.

#!/usr/bin/env ruby

require 'fileutils'

FileUtils.mv('/tmp/your_file', '/opt/new/location/your_file')

Remember; if you are moving across partitions, "mv" will copy the file to new destination and unlink the source path.

Berk D. Demir
I forgot to add that you can not move across partitions. Thanks.
Željko Filipin
Actually mv does the copy for you. "Moves file(s) src to dest. If file and dest exist on the different disk partition, the file is copied instead." ... http://www.ruby-doc.org/core/classes/FileUtils.html#M004330
Darkerstar
Darkerstar: Thank you. I edited the reply.
Berk D. Demir
can you confirm if the file is deleted after being copied from a different partition?
knoopx