How do I move a file with Ruby?

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


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.


An old question, i'm surprised no one answered this simple solution. You don't need fileutils or a systemcall, just rename the file to the new location.

File.rename source_path, target_path

Happy coding


FileUtils.move

require 'fileutils'
FileUtils.move 'stuff.rb', '/notexist/lib/ruby'