ruby 中获取可用磁盘空间

发布于 2024-10-08 11:28:56 字数 109 浏览 4 评论 0原文

使用 ruby​​ 获取磁盘空间信息的最佳方法是什么? 我更喜欢纯红宝石解决方案。如果不可能(即使有额外的 gems),它也可以使用标准 ubuntu 桌面安装中可用的任何命令将信息解析为 ruby​​。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

闻呓 2024-10-15 11:28:56

您可以使用 sys-filesystem gem (跨平台友好)

require 'sys/filesystem'

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

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
星光不落少年眉 2024-10-15 11:28:56

简单一点怎么样:

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

其中“/dev/sda1”是路径,通过简单地运行 df 来确定

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

多情癖 2024-10-15 11:28:56

(Ruby) Daniel Berger 在这个领域维护着很多瑰宝。在那里可以找到:sys-cpu、sys-uptime、sys-uname、sys-proctable、sys-host、sys-admin、sys-filesystem。他们(据我所知)是多平台的。

(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.

拍不死你 2024-10-15 11:28:56

您好,我为此创建了 gem: https://github.com/pr0d1r2/free_disk_space

您可以使用它by:

gem 'free_disk_space' # 添加一行到 Gemfile

内部代码使用方法:

FreeDiskSpace.terabytes('/')

FreeDiskSpace.gigabytes('/')

FreeDiskSpace.megabytes('/')

FreeDiskSpace.kilobytes('/')

FreeDiskSpace.字节('/')

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('/')

柠檬 2024-10-15 11:28:56

这是 dkams 答案的扩展,没有错误,但计算了一个驱动器的完整空间,以检查剩余的可用空间。即驱动器上的可用空间替换 kdams secdn 行如下:

gb_available = stat.bytes_free / 1024 / 1024 / 1024

这将返回剩余的可用空间在你的演出中。

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.

鹿童谣 2024-10-15 11:28:56

这仅适用于 Linux 系统:如果您不介意调用 shell,您可以使用 df 作为文件系统并使用正则表达式解析输出:

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)

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)
旧城空念 2024-10-15 11:28:56

类似于 rogerdpack 的评论
要获得 GB / MB 的可用空间,您可以尝试以下操作

# 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

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
勿忘心安 2024-10-15 11:28:56
def check_disk_space
  system('df -H | grep debug > ff')
  ss = File.open('ff').read.split(/\s+/)
  system('rm ff')
  "#{ss[3]}"
end

在ubuntu下使用,检查调试大小,将可用大小作为输出。

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.

感情旳空白 2024-10-15 11:28:56

我相信这个更加稳健。

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)

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)
哑剧 2024-10-15 11:28:56

我的无 gem 解决方案(仅限 Linux):

storage_info = `df -h | grep "sda1" -w`

# storage_info => "/dev/sda1        39G   27G   12G  70% /"

然后根据您可能需要的确切信息(总空间、已用空间、可用空间、已用空间百分比),您可以执行以下操作:

free_space = storage_info.split(" ")[3]

# free_space => "12G"

My gem-free solution (linux only):

storage_info = `df -h | grep "sda1" -w`

# storage_info => "/dev/sda1        39G   27G   12G  70% /"

then depending on the exact info you may need (total space, used space, free space, used space in percentage) you could do:

free_space = storage_info.split(" ")[3]

# free_space => "12G"
野の 2024-10-15 11:28:56

一个无宝石的解决方案,以字节为单位回答:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文