# Human readable file sizes

**URL:** <https://www.ruby-forum.com/t/human-readable-file-sizes/116311>\
**Category:** Ruby\
**Created:** [October 4, 2007, 12:06pm UTC](https://www.ruby-forum.com/t/human-readable-file-sizes/116311 "2007-10-04T12:06:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![JesSSSSs\_Gabriel\_y\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4da419/32.png) [@JesSSSSs\_Gabriel\_y\_S](https://www.ruby-forum.com/u/JesSSSSs_Gabriel_y_S)\
**Post date:** [October 4, 2007, 12:06pm UTC](https://www.ruby-forum.com/t/human-readable-file-sizes/116311/1 "2007-10-04T12:06:13Z")

</div>

Hi,

I have a list of files and I need to produce a string containing the  
size of each file, but in human readable format. With that I mean  
something like what df -h does: make an approximation to the nearest  
unit (B, KB, MB, GB, etc). Is this already done somewhere? This is  
what I have now:

Find.find(folder) do |file|  
match = regexp.match(File.basename(file));  
if match  
size = File.stat(file).size  
puts size # would like a human readable format  
end  
end

Thanks,

Jesus.

---

<div class="post-metadata">

**Author:** ![JesSSSSs\_Gabriel\_y\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4da419/32.png) [@JesSSSSs\_Gabriel\_y\_S](https://www.ruby-forum.com/u/JesSSSSs_Gabriel_y_S)\
**Post date:** [October 4, 2007, 12:20pm UTC](https://www.ruby-forum.com/t/human-readable-file-sizes/116311/2 "2007-10-04T12:20:51Z")

</div>

I think this is what you want

GIGA\_SIZE = 1073741824.0  
MEGA\_SIZE = 1048576.0  
KILO\_SIZE = 1024.0

# Return the file size with a readable style.

def readable\_file\_size(size, precision)  
case  
when size == 1 : “1 Byte”  
when size \< KILO\_SIZE : “%d Bytes” % size  
when size \< MEGA\_SIZE : “%.#{precision}f KB” % (size / KILO\_SIZE)  
when size \< GIGA\_SIZE : “%.#{precision}f MB” % (size / MEGA\_SIZE)  
else “%.#{precision}f GB” % (size / GIGA\_SIZE)  
end  
end

---

<div class="post-metadata">

**Author:** ![JesSSSSs\_Gabriel\_y\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4da419/32.png) [@JesSSSSs\_Gabriel\_y\_S](https://www.ruby-forum.com/u/JesSSSSs_Gabriel_y_S)\
**Post date:** [October 4, 2007, 1:14pm UTC](https://www.ruby-forum.com/t/human-readable-file-sizes/116311/3 "2007-10-04T13:14:48Z")

</div>

> something like what df -h does: make an approximation to the nearest
> 
> Thanks,
> 
> Jesus.

ls -Qsh1

HTH,

Felix

---

<div class="post-metadata">

**Author:** ![JesSSSSs\_Gabriel\_y\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4da419/32.png) [@JesSSSSs\_Gabriel\_y\_S](https://www.ruby-forum.com/u/JesSSSSs_Gabriel_y_S)\
**Post date:** [October 4, 2007, 4:35pm UTC](https://www.ruby-forum.com/t/human-readable-file-sizes/116311/4 "2007-10-04T16:35:56Z")

</div>

Jesús Gabriel y Galán schrieb:

> ```
> if match
> size = File.stat(file).size
> puts size # would like a human readable format
> end
> 
> ```
> 
> end
> 
> Thanks,
> 
> Jesus.

Like this:

* * *

## class Numeric def to\_human units = %w{B KB MB GB TB} e = (Math.log(self)/Math.log(1024)).floor s = “%.3f” % (to\_f / 1024\*\*e) s.sub(/.?0\*$/, units[e]) end end

Note: this may be not 100% correct for very large values (like TB)  
because  
Math.log uses floating point arithmetic.

cheers

Simon

---

<div class="post-metadata">

**Author:** ![JesSSSSs\_Gabriel\_y\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4da419/32.png) [@JesSSSSs\_Gabriel\_y\_S](https://www.ruby-forum.com/u/JesSSSSs_Gabriel_y_S)\
**Post date:** [October 4, 2007, 2:53pm UTC](https://www.ruby-forum.com/t/human-readable-file-sizes/116311/5 "2007-10-04T14:53:51Z")

</div>

On 10/4/07, Felix W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > -----Original Message-----  
> > From: Jesús Gabriel y Galán [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

> > ```
> > puts size # would like a human readable format
> > end
> > 
> > ```
> > 
> > end
> > 
> > Thanks,
> > 
> > Jesus.
> 
> ls -Qsh1

Sorry, the above requirements are a simplification. For one I need to  
retrieve only the file names that match a regexp, then depending on  
parameters I need to gzip or delete the files and send an email with  
the report of everything that was done and the free space before and  
after in the disk, and some more things. So I’m writing a ruby program  
to do that. I have a loop similar to the one above that does more  
things, but one of them is to retrieve the file size for reporting,  
and I would like to show human readable format a la -h flag for ls,  
df, etc.

If there’s no solution already implemented in places like File,  
FileUtils or so (I haven’t found it) I’ll go with a custom solution  
similar to the solution posted by Huang Zhimin.

Thanks,

Jesus.

---

<div class="post-metadata">

**Author:** ![JesSSSSs\_Gabriel\_y\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4da419/32.png) [@JesSSSSs\_Gabriel\_y\_S](https://www.ruby-forum.com/u/JesSSSSs_Gabriel_y_S)\
**Post date:** [October 4, 2007, 6:01pm UTC](https://www.ruby-forum.com/t/human-readable-file-sizes/116311/6 "2007-10-04T18:01:46Z")

</div>

On Oct 4, 5:05 am, “Jesús Gabriel y Galán” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> if match
> size = File.stat(file).size
> puts size # would like a human readable format
> end
> 
> ```
> 
> end
> 
> Thanks,
> 
> Jesus.

def kilo n  
count = 0  
while n \>= 1024 and count \< 4  
n /= 1024.0  
count += 1  
end  
format(“%.2f”,n) + %w(B KB MB GB TB)[count]  
end

[1,1024,1\_000\_000,8\_000\_000,9\_000\_000\_000,  
3\_000\_000\_000\_000].each{|n|  
puts kilo(n)  
}

---

<div class="post-metadata">

**Author:** ![JesSSSSs\_Gabriel\_y\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4da419/32.png) [@JesSSSSs\_Gabriel\_y\_S](https://www.ruby-forum.com/u/JesSSSSs_Gabriel_y_S)\
**Post date:** [October 4, 2007, 5:28pm UTC](https://www.ruby-forum.com/t/human-readable-file-sizes/116311/7 "2007-10-04T17:28:03Z")

</div>

JesÃºs Gabriel y GalÃ¡n wrote:

> I have a list of files and I need to produce a string containing the  
> size of each file, but in human readable format. With that I mean  
> something like what df -h does: make an approximation to the nearest  
> unit (B, KB, MB, GB, etc). Is this already done somewhere?

> **[ActionView::Helpers::NumberHelper](https://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#M000525)**
>
> Provides methods for converting numbers into formatted strings. Methods are provided for phone numbers, currency, percentage, precision, positional notation, file size, and pretty printing.

---

<div class="post-metadata">

**Author:** ![JesSSSSs\_Gabriel\_y\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4da419/32.png) [@JesSSSSs\_Gabriel\_y\_S](https://www.ruby-forum.com/u/JesSSSSs_Gabriel_y_S)\
**Post date:** [October 5, 2007, 8:25am UTC](https://www.ruby-forum.com/t/human-readable-file-sizes/116311/8 "2007-10-05T08:25:34Z")

</div>

On 10/4/07, Daniel W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Jesús Gabriel y Galán wrote:
> 
> > I have a list of files and I need to produce a string containing the  
> > size of each file, but in human readable format. With that I mean  
> > something like what df -h does: make an approximation to the nearest  
> > unit (B, KB, MB, GB, etc). Is this already done somewhere?
> 
> [ActionView::Helpers::NumberHelper](http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#M000525)

Thanks to everybody who answered. I think I’ll go with the action view  
helper, cause it’s already done :-).

Thanks again,

Jesus.
