Skip to content

Instantly share code, notes, and snippets.

@kminiatures
Created May 17, 2016 04:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kminiatures/f8a920e0dd2dea52a7a5adde0179f2a9 to your computer and use it in GitHub Desktop.
Save kminiatures/f8a920e0dd2dea52a7a5adde0179f2a9 to your computer and use it in GitHub Desktop.
find heavy file
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