From: Mathieu Bouchard Date: 2002-01-01T09:09:53+09:00 Subject: [ruby-talk:29915] Re: How to check free diskspace? (fwd) On Mon, 31 Dec 2001, Mathieu Bouchard wrote: > On Tue, 1 Jan 2002, Le Wang wrote: > > Hi all, > > I've checked "Programming Ruby", and searched the ML to no avail. So > > how do you check free disk space, other than to parse 'df' output? > Mount = Struct.new( > :device, > :path, > :fs_type, > :options, > :freq, > :passno) > mounts = File.open("/etc/mtab") {|f| > f.map {|x| > Mount.new(*x.chomp.split) }} Er... I forgot to finish the code. This only tells what's mounted and where. I realized that, unfortunately, the functionality you really want is system-dependent. It doesn't seem to be covered by POSIX. GNU's "df" command (in fileutils package) can use any of several implementations, including BSD, SysV, AIX, ... Here's a new version which includes df support. Mount = Struct.new( :device, :path, :fs_type, :options, :freq, :passno, :total, :used, :free) mounts = File.open("/etc/mtab") {|f| f.map {|x| m = Mount.new(*x.chomp.split) m.freq=m.freq.to_i m.passno=m.passno.to_i m }} File.popen("sh -c 'LC_ALL=en df -P'") {|f| 2.times { f.readline } f.map{|r| dev,*stuff = r.chomp.split /\s+/ m = mounts.find {|d| d.device == dev } m.total,m.used,m.free = stuff[0..2].map{|x| x.to_i }}} ________________________________________________________________ Mathieu Bouchard http://hostname.2y.net/~matju