[ruby] Ruby로 파일을 어떻게 이동합니까?

Ruby로 파일을 이동하고 싶습니다. 어떻게합니까?



답변

FileUtils를 사용하여이를 수행 할 수 있습니다.

#!/usr/bin/env ruby

require 'fileutils'

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

생각해 내다; 파티션간에 이동하는 경우 “mv”는 파일을 새 대상으로 복사하고 소스 경로를 연결 해제합니다.


답변

오래된 질문, 아무도이 간단한 해결책에 대답하지 않은 것에 놀랐습니다. fileutils 또는 시스템 호출이 필요하지 않으며 파일 이름을 새 위치로 바꾸십시오.

File.rename source_path, target_path

행복한 코딩


답변

FileUtils.move

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


답변

‘fileutils’모듈을 사용하고 FileUtils.mv를 사용하십시오.

http://www.ruby-doc.org/stdlib-2.0/libdoc/fileutils/rdoc/FileUtils.html#method-c-mv


답변

여기 템플릿이 있습니다.

 src_dir = "/full_path/to_some/ex_file.txt"

 dst_dir = "/full_path/target_dir"

 #Use the method below to do the moving
 move_src_to_target_dir(src_dir, dst_dir)



 def archive_src_to_dst_dir(src_dir, dst_dir)

     if File.exist ? (src_dir)

     puts "about to move this file:  #{src_dir}"

     FileUtils.mv(src_dir, dst_dir)
 else

     puts "can not find source file to move"

 end
 end


답변

이처럼 파일을 이동할 수 있습니다

Rails.root.join ( ‘foo’, ‘bar’)


답변