finding size of directories

I wanted to find out the size taken by some directories in a given folder. I didn’t like what du gave me, so I wrote this script :

require "find"

# directory we want to list size for
where = ARGV[0]
# open it
Dir.open(where) do |dir|
	# iterate
	dir.each do |subfile|		
		# get a full path
		subfile = File.join(where,subfile)
		# initialize this to 0 for each new file
		size = 0
		# test to see if it's a directory
		if File.directory?(subfile)
			# if it's a dir, we need to add the size of each of the files there
			Find.find(subfile) do |file|
				# skip if it's a dir
				next if File.directory?(file)
				# add the size
				# we rescue this, because some files might be locked
				size += File.stat(file).size rescue 0
			# this is a regular file, we initialize size to this file's size
			size = File.stat(subfile).size rescue 0
		# we print
		puts "size for #{subfile} is %.2f MB" % (size/(1024.0*1024.0))


0 Responses to “finding size of directories”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Blog Stats

  • 238,127 hits

%d bloggers like this: