Created
October 9, 2008 02:32
-
-
Save Roman2K/15677 to your computer and use it in GitHub Desktop.
This file contains 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
#!/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