I want to move a file with ruby. How do I do that?
+1
A:
require "FileUtils"
FileUtils.move 'stuff.rb', '/notexist/lib/ruby'
Željko Filipin
2008-12-31 15:43:22
+1 Note that .move is an alias of .mv so you should be able to do FileUtils.mv too.
Tim K.
2008-12-31 15:45:58
Thanks, I just like .move more that .mv. :)
Željko Filipin
2008-12-31 15:49:59
I don't. `mv` makes me feel like I'm in my beloved console ;)
Erik Escobedo
2010-09-03 16:45:47
And the nice thing about Ruby is that it has both `move` and `mv`so one can pick either one. :)
Željko Filipin
2010-09-06 09:00:00
+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
2008-12-31 15:46:57
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
2010-05-16 07:23:17
can you confirm if the file is deleted after being copied from a different partition?
knoopx
2010-08-18 12:54:44