Ruby 에서 여러 디렉토리의 사이즈를 구하고 크기 순으로 정렬하는 방법을 구현해보았다. 여러 팀에서 공유하는 파일의 서버의 경우, 각 디렉토리별(팀)로 어느 정도 사용하는 지를 알아보고 싶을 때 이용하면 좋을 것이다.

require 'find'


$FOLDER_LIST = [
                'C:\\path\\to1',
                'C:\\path\\to2',
                'C:\\path\\to3',
                'C:\\path\\to4',
                'C:\\path\\to5',
               ]

start_time = Time.now


size_folder_list = Array.new
$FOLDER_LIST.each() do | folder_name |
  folder_size = 0
  Find.find(folder_name) do | path |
    if(File.exist?(path))
      folder_size += File.size(path)
    end
  end
  size_folder_list << "#{folder_size}|#{folder_name}"
end

size_folder_list = size_folder_list.sort do |a, b| 
  (a_size, a_file) = a.split(/\|/)
  (b_size, b_file) = b.split(/\|/)
  b_size.to_i <=> a_size.to_i
end

size_folder_list.each do | folder |
  (f_size, f_path) = folder.split(/\|/)
  print f_size + "," + f_path + "\n"
end


end_time = Time.now
puts "\n\n========================================================================="
puts "Start Time : #{start_time}"
puts "End Time   : #{end_time}"

다음은 특정 디렉토리 아래에 있는 파일 중에서 200MB 넘는 파일을 찾아서, 크기로 정렬하여 출력하는 프로그램이다. 어려운 것은 아니지만, 필요할 때 찾아보면 좋을 듯 하여~ ^^

# -*- coding: cp949 -*-
require 'find'

$TARGET     = "C:\\"
$SIZE_LIMIT = 200_000_000

start_time = Time.now

puts "Script Start..."
puts "=========================================================================\n\n"

file_list = Array.new
Find.find($TARGET) do |path|
  if File.file?(path) and File.size(path) > $SIZE_LIMIT
    file_size = File.size(path)
    file_list << file_size.to_s + "|" + path
  end
end


file_list = file_list.sort do |a, b| 
  (a_size, a_file) = a.split(/\|/)
  (b_size, b_file) = b.split(/\|/)
  b_size.to_i <=> a_size.to_i
end


file_list.each do |file|
  (f_size, f_path) = file.split(/\|/)
  print f_size + " -> " + f_path + "\n"
end


end_time = Time.now


puts "\n\n========================================================================="
puts "Start Time : #{start_time}"
puts "End Time   : #{end_time}"

puts "Script End..."

+ Recent posts