Skip to content

Instantly share code, notes, and snippets.

@Roman2K
Created October 9, 2008 02:32
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 Roman2K/15677 to your computer and use it in GitHub Desktop.
Save Roman2K/15677 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Sort entries by content size. Usage:
# sortbysize [<directory>='.']
#
# This script is documented at:
# http://roman.flucti.com/sorting-directories-by-content-size-with-ruby
#
class Entry < Struct.new(:size, :unit, :name)
UNITS = %w(B K M G T P)
FORMAT = /^(.+)(#{UNITS * '|'})\t+(.+)$/
def self.parse_list(du_output)
du_output.split($/).map { |line| Entry.parse(line) }.compact
end
def self.parse(line)
return unless line =~ FORMAT
new($1.to_f, $2, $3)
end
def <=>(other)
return unless self.class === other
comparable <=> other.comparable
end
def to_s
"%6s%s %s" % [size, unit, name]
end
protected
def comparable
[UNITS.index(unit), size, name]
end
end
Dir.chdir(ARGV.first || '.') do
puts Entry.parse_list(`du -sh *`).sort.reverse
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment