Get available diskspace in ruby

此生再无相见时 提交于 2019-12-03 14:51:00

问题


What is the best way to get diskspace information with ruby. I would prefer a pure ruby solution. If not possible (even with additional gems), it could also use any command available in a standard ubuntu desktop installation to parse the information into ruby.


回答1:


You could use the sys-filesystem gem (cross platform friendly)

require 'sys/filesystem'

stat = Sys::Filesystem.stat("/")
mb_available = stat.block_size * stat.blocks_available / 1024 / 1024



回答2:


How about simply:

spaceMb_i = `df -m /dev/sda1`.split(/\b/)[24].to_i

where '/dev/sda1' is the path, determined by simply running df




回答3:


(Ruby) Daniel Berger maintains a lot of gems in this field. To be found there: sys-cpu, sys-uptime, sys-uname, sys-proctable, sys-host, sys-admin, sys-filesystem. They are (AFAIK) multi-platform.




回答4:


Hi i have created gem for that: https://github.com/pr0d1r2/free_disk_space

You can use it by:

gem 'free_disk_space' # add line to Gemfile

Inside code use methods:

FreeDiskSpace.terabytes('/')

FreeDiskSpace.gigabytes('/')

FreeDiskSpace.megabytes('/')

FreeDiskSpace.kilobytes('/')

FreeDiskSpace.bytes('/')




回答5:


This is an extension to dkams answer which is not wrong, but calculated the complete space of ones drive, to check for the remaining usable .i.e. the FREE space on a drive substitute kdams secodn line with the following:

gb_available = stat.bytes_free / 1024 / 1024 / 1024

This will return the remaining free space on your drive in Gigs.




回答6:


Similar to comment rogerdpack's comment to get the space free in GB / MB you may try following

# Get free space in Gb in present partition
gb_free = `df -BG .`.split[10].to_i**

# Get free space in MB in /dev/sda1 partition
mb_free = `df -BM /dev/sda1`.split[10].to_i**
puts  gb_free, mb_free



回答7:


This works only on a Linux system: If you don't mind calling out to the shell, you can use df for a filesystem and parse the output with a Regexp:

fs_to_check = '/boot'
df_output = `df #{fs_to_check}`
disk_line = df_output.split(/\n/)[1]
disk_free_bytes = disk_line.match(/(.+)\s+(\d+)\s+(\d+)\s+(\d+)\s+/)[4].to_i
disk_free_mbs = disk_free_bytes / 1024
puts(disk_free_mbs)



回答8:


def check_disk_space
  system('df -H | grep debug > ff')
  ss = File.open('ff').read.split(/\s+/)
  system('rm ff')
  "#{ss[3]}"
end

Used under ubuntu, to check debugs size,put the available size as output.




回答9:


I believe this is more robust.

filesystem = "/dev/sda1"
free_megabytes = `LANG=C df -m #{filesystem}`.split("\n").map do |line|
  line.split.first(4)
end.transpose.to_h["Available"]&.to_i
puts(free_megabytes)



回答10:


A gem free solution, answer in bytes:

(File.exists?('C:\\') ? `dir /-C`.match(/(\d+) bytes free/) : `df .`.match(/(\d+)\s*\d*%/)).captures[0].to_i


来源:https://stackoverflow.com/questions/4508692/get-available-diskspace-in-ruby

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!