Techioz Blog

Rubyまたはシェルスクリプトを使用して別のフォルダ内でフォルダを移動しますか?

概要

私はVijayという共通のフォルダー名を持っており、その中にはいくつかのフォルダーがあります。

フォルダー名の例:

32,032,055,056,095。

32という名前のフォルダを032フォルダ内に移動するにはどうすればよいですか。

Ruby スクリプトまたはシェル スクリプトが必要です。

解決策

次のようにすることができます:

require 'fileutils'
FileUtils.cp_r("path_to_vijay/32/","path_to_vijay/032/")

ドキュメントを読む::cp_r

例 :-

require 'fileutils'

# see here the test2 directoy is empty
Dir.glob("#{__dir__}/test2/**/*") # => []
# look at the content of the test1 directory, which will be copied by it
# parent directory to test2
Dir.glob("#{__dir__}/test1/**/*") # => ["/home/arup/Ruby/test1/a.rb"]

FileUtils.cp_r("#{__dir__}/test1/","#{__dir__}/test2/")

# see the test1 directory itself got copied with all its contents to the
# test2/ directory
Dir.glob("#{__dir__}/test2/**/*")  
# => ["/home/arup/Ruby/test2/test1", "/home/arup/Ruby/test2/test1/a.rb"]