Skip to content

Instantly share code, notes, and snippets.

@thefury
Created August 17, 2017 17:22
Show Gist options
  • Save thefury/2a498b8d8d24c37734767d4bef66e6d8 to your computer and use it in GitHub Desktop.
Save thefury/2a498b8d8d24c37734767d4bef66e6d8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'optparse'
require 'ostruct'
require 'filesize'
require 'pry'
opts = {}
opts[:path] = '/'
opts[:number] = 15
OptionParser.new do |p|
p.on("-v", "--verbose", "verbose output") do |v|
opts[:verbose] = v
end
p.on("-p", "--path PATH", "report large files starting at PATH (default '/')") do |v|
opts[:path] = v
end
p.on("-n", "--number N", "report the largest N files (default 15)") do |v|
opts[:number] = v
end
end.parse!
if opts[:verbose]
puts "scanning #{opts[:path]} for #{opts[:number]} largest files..."
end
# Not using the du -h option because sort doesn't handle -h on a mac
`sudo du -a #{opts[:path]} | sort -rn | head -n #{opts[:number]}`.each_line do |line|
size = Filesize.from(line.split("\t").first.strip + " B").pretty
file = line.split("\t").last.strip
puts "#{size}\t#{file}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment