Skip to content

Instantly share code, notes, and snippets.

@marshallmick007
Created July 3, 2017 15:08
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 marshallmick007/a6ada717a342d201b18ba36fe40435b9 to your computer and use it in GitHub Desktop.
Save marshallmick007/a6ada717a342d201b18ba36fe40435b9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# encoding: utf-8
begin
require 'rainbow'
rescue LoadError
puts "Rainbow is not installed"
puts " gem install rainbow"
exit
end
class Integer
def to_filesize
{
'B' => 1024,
'KB' => 1024 * 1024,
'MB' => 1024 * 1024 * 1024,
'GB' => 1024 * 1024 * 1024 * 1024,
'TB' => 1024 * 1024 * 1024 * 1024 * 1024
}.each_pair { |e, s| return "#{(self.to_f / (s / 1024)).round(3)}#{e}" if self < s }
end
end
class DisplayItem
attr_accessor :size, :location, :type
@@base_dir=''
def initialize(size, location, type)
@size = size
@location = location.strip
@type = type
end
def self.for_file(line)
toks = line.split(':')
DisplayItem.new(toks[0].to_i, toks[1..-1].join(':'), :file)
end
def self.for_folder(line)
toks = line.split(' ')
DisplayItem.new(toks[0].to_i, toks[1..-1].join(' '), :folder)
end
def self.set_base_dir(dir)
@@base_dir = dir
end
def to_s
display_size + display_location
end
def display_size
Rainbow(@size.to_filesize.ljust(12)).color(display_size_color)
end
def display_size_color
if @size > 100 * 1024**2
:red
else
:yellow
end
end
def display_location
loc = @location.sub(@@base_dir, '.')
if @type == :folder
Rainbow(loc).green
else
loc
end
end
end
dir = Dir.pwd
DisplayItem.set_base_dir dir
puts "Scanning #{dir}..."
fldr = `command du -ab #{dir} | sort -n -r | head -n 10`.split("\n")
files = %x[find #{dir} -type f -size +10000k -exec ls -l "{}" "\;" | awk '{print \$5 \": \" \$9}'].split("\n")
items = []
fldr.each { |f| items << DisplayItem.for_folder(f) }
files.each { |f| items << DisplayItem.for_file(f) }
items.sort! {|a,b| b.size <=> a.size }
puts items
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment