Get available diskspace in ruby
Asked Answered
R

11

29

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.

Retrad answered 22/12, 2010 at 11:27 Comment(1)
under windows or linux? , if windows there is a Question for that #3259018Otis
U
40

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
Uglify answered 28/5, 2011 at 11:44 Comment(1)
is there any way to get the total remaining size of root disk in ruby for Linux and Windows without using any gem?Monochromatic
C
9

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

Czarina answered 21/1, 2011 at 21:16 Comment(2)
Didn't work for me for some reason (the split?) but this did bytes_free = `df -B1 .`.split[10].to_iLittlest
is there any way to get the total remaining size of root disk in ruby for Linux and Windows without using any gem?Monochromatic
S
5

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

Sorcerer answered 4/1, 2012 at 1:23 Comment(0)
N
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('/')

Necrosis answered 16/11, 2013 at 16:8 Comment(3)
"While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes."Semester
gems not updated in 7 years are not helpfullDoited
The gem generates a class based on the current accepted response so it seems safe to use @fatfrog, or to copy that file to your project.Danieu
O
4

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.

Outwards answered 11/6, 2015 at 11:40 Comment(7)
If it's in Gigs why call it mb_available then?Barsac
@Barsac that would be because of how the params are 1024 / 1024 / 1024 where it goes from byte, megabyte, gigabyte. Which seeing bytes_free should have been a dead give away. mb_available is because this is the standard way to display any amount of data unless of course its below 1 mb. Although with the params you have control of how you want it (KB, MB, GB).Undistinguished
@Undistinguished If bytes_free returns bytes (which it does), and then we divide it by 1024 three times we get: bytes -> kilobytes -> megabytes -> gigabytes. So @zachaysan's question is why the variable is named mb_available instead of the more accurate gb_available.Astute
@Astute I'm confused now on how GB would be more accurate than a smaller more precise value? All he did was assign mb_available to total bytes / (divided by) 1024 to get the gigs available. The original question/answer had nothing to do with GB, idk why this one does... Kilobyte (KB) 1,024 Bytes Megabyte (MB) 1,024 Kilobytes Starts in bytes: 1024 BYTES; 1024 KILOBYTES; 1024 MEGABYTES Where the resulting value would be less than needed to move up into 1024 GIGABYTES values.Undistinguished
@Undistinguished I would expect a variable called mb_available to contain a number that represents the number of megabytes available, and I would expect a variable called gb_available to contain a number that represents the number of gigabytes available. But in the answer above, we have "gigabytes available" stored in a variable which has a name that implies it's actually storing "megabytes available" - this is misleading and should be avoided.Astute
Hi Guys, yes i made mistake, thank you, the var should be called gb_available.Outwards
is there any way to get the total remaining size of root disk in ruby for Linux and Windows without using any gem?Monochromatic
A
1

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
Alcoholic answered 23/6, 2016 at 9:52 Comment(1)
What's the double asterisk (**) about?Diamond
J
1

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)
Jobye answered 31/10, 2016 at 10:38 Comment(1)
is there any way to get the total remaining size of root disk in ruby for Linux and Windows without using any gem?Monochromatic
M
0
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.

Muffle answered 14/7, 2013 at 2:20 Comment(0)
F
0

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)
Fricke answered 15/4, 2019 at 4:42 Comment(0)
E
0

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"
Emir answered 19/10, 2022 at 23:6 Comment(0)
H
-4

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
Harragan answered 12/1, 2016 at 6:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.