Created
May 17, 2016 04:21
-
-
Save kminiatures/f8a920e0dd2dea52a7a5adde0179f2a9 to your computer and use it in GitHub Desktop.
find heavy file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'fileutils' | |
class FindHeavyFile | |
def self.find(depth = 0) | |
size, file = `du -sh *`.strip.split("\n").max_by do |f| | |
size,file = f.split "\t" | |
to_bytes(size) | |
end.split("\t") | |
puts "#{' ' * depth}#{size} #{file}" | |
if File.directory?(file) | |
FileUtils.cd(file) | |
find(depth + 1) | |
end | |
end | |
def self.to_bytes(str) | |
unit = str[-1] | |
if unit =~ /[0-9]/ | |
unit = nil | |
size = str[0..-1].to_i | |
else | |
size = str[0...-1].to_i | |
end | |
case unit | |
when "M"; (size * 1024).to_i | |
when "K"; (size * 1024 ** 2).to_i | |
when "G"; (size * 1024 ** 3).to_i | |
else | |
size.to_i | |
end | |
end | |
end | |
FindHeavyFile.find |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment