Method: FileUtils#mv

Defined in:
lib/fileutils.rb

#mv(src, dest, options = {}) ⇒ Object Also known as: move

Options: force noop verbose

Moves file(s) src to dest. If file and dest exist on the different disk partition, the file is copied then the original file is removed.

FileUtils.mv 'badname.rb', 'goodname.rb'
FileUtils.mv 'stuff.rb', '/notexist/lib/ruby', :force => true  # no error

FileUtils.mv %w(junk.txt dust.txt), '/home/aamine/.trash/'
FileUtils.mv Dir.glob('test*.rb'), 'test', :noop => true, :verbose => true


500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# File 'lib/fileutils.rb', line 500

def mv(src, dest, options = {})
  fu_check_options options, OPT_TABLE['mv']
  fu_output_message "mv#{options[:force] ? ' -f' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
  return if options[:noop]
  fu_each_src_dest(src, dest) do |s, d|
    destent = Entry_.new(d, nil, true)
    begin
      if destent.exist?
        if destent.directory?
          raise Errno::EEXIST, dest
        else
          destent.remove_file if rename_cannot_overwrite_file?
        end
      end
      begin
        File.rename s, d
      rescue Errno::EXDEV
        copy_entry s, d, true
        if options[:secure]
          remove_entry_secure s, options[:force]
        else
          remove_entry s, options[:force]
        end
      end
    rescue SystemCallError
      raise unless options[:force]
    end
  end
end