Skip to content

Instantly share code, notes, and snippets.

@dkam
Created May 19, 2023 04:10
Show Gist options
  • Save dkam/440bb941b9cdeae5ef333d2e144d3fc3 to your computer and use it in GitHub Desktop.
Save dkam/440bb941b9cdeae5ef333d2e144d3fc3 to your computer and use it in GitHub Desktop.
Get free disk space
#!/usr/bin/env ruby
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'json'
gem 'sys-filesystem'
end
require 'sys/filesystem'
def fs_mounts
File.read('/etc/fstab').lines.select {|l| l.start_with?('/') }.map do |ml|
next if ml.split(' ')[2] == 'swap'
ml.split(' ')[1]
end.compact
end
def sys_mounts(mtypes: ['ext4']) = Sys::Filesystem.mounts.select {|m| mtypes.include? m.mount_type }
def space_available(mount, unit: :mb)
stat = Sys::Filesystem.stat(mount)
percent = 100 - (100.0 * stat.bytes_available.to_f / stat.bytes_total.to_f)
[stat.bytes_available / 1024 / 1024, percent ]
end
sys_mounts.each do |mount|
puts "'#{mount.mount_point}' -> %d MB available ; %0.1f %% full" % space_available(mount.mount_point)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment