使用ruby获取磁盘空间信息的最佳方法是什么。 我更喜欢纯红宝石溶液。如果不可能(即使有额外的宝石),它也可以使用标准ubuntu桌面安装中可用的任何命令将信息解析为ruby。
答案 0 :(得分:33)
您可以使用sys-filesystem gem(跨平台友好)
require 'sys/filesystem'
stat = Sys::Filesystem.stat("/")
mb_available = stat.block_size * stat.blocks_available / 1024 / 1024
答案 1 :(得分:7)
简单地说:
spaceMb_i = `df -m /dev/sda1`.split(/\b/)[24].to_i
其中'/ dev / sda1'是路径,只需运行df
即可确定答案 2 :(得分:4)
(Ruby)Daniel Berger在此字段中维护a lot of gems。在那里找到:sys-cpu,sys-uptime,sys-uname,sys-proctable,sys-host,sys-admin,sys-filesystem。他们是(AFAIK)多平台。
答案 3 :(得分:4)
您好我为此创建了宝石:https://github.com/pr0d1r2/free_disk_space
gem'free_disk_space'#将行添加到Gemfile
FreeDiskSpace.terabytes( '/')
FreeDiskSpace.gigabytes( '/')
FreeDiskSpace.megabytes( '/')
FreeDiskSpace.kilobytes( '/')
FreeDiskSpace.bytes( '/')
答案 4 :(得分:4)
这是dkams回答的扩展,没有错,但是计算了一个驱动器的完整空间,以检查剩余的可用空间.i.e。驱动器上的免费空间用以下代码替换kdams secodn行:
gb_available = stat.bytes_free / 1024 / 1024 / 1024
这将返回Gigs中驱动器上剩余的可用空间。
答案 5 :(得分:1)
def check_disk_space
system('df -H | grep debug > ff')
ss = File.open('ff').read.split(/\s+/)
system('rm ff')
"#{ss[3]}"
end
在ubuntu下使用,检查调试大小,将可用大小作为输出。
答案 6 :(得分:1)
与评论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
答案 7 :(得分:1)
这仅适用于Linux系统:如果您不介意调用shell,可以使用df
作为文件系统,并使用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 :(得分:0)
我相信这会更强大。
<script type="text/javascript">
function myFunction(){
var hsh = document.getElementById("show");
if ( hsh.value === "******" )
hsh.value = "<?php echo $contact; ?>";
else
hsh.value = "Open Curtain";
}
</script>
答案 9 :(得分:-3)
无宝石解决方案,以字节为单位回答:
(File.exists?('C:\\') ? `dir /-C`.match(/(\d+) bytes free/) : `df .`.match(/(\d+)\s*\d*%/)).captures[0].to_i