Skip to content

Instantly share code, notes, and snippets.

@sagarbommidi
Created November 20, 2018 02:33
Show Gist options
  • Save sagarbommidi/0b26594adf8348a391a1d71e841f67d7 to your computer and use it in GitHub Desktop.
Save sagarbommidi/0b26594adf8348a391a1d71e841f67d7 to your computer and use it in GitHub Desktop.
It will find all the log files from the specified folder recursively and also their size, and asks the user whether to delete it or not.
require 'find'
def format_mb(size)
conv = [ 'b', 'kb', 'mb', 'gb', 'tb', 'pb', 'eb' ];
scale = 1024;
ndx = 1
if( size < 2*(scale**ndx) )
return "#{(size)} #{conv[ndx-1]}"
end
size = size.to_f
[2,3,4,5,6,7].each do |ndx|
if ( size < 2*(scale**ndx) )
return "#{'%.3f' % (size/(scale**(ndx-1)))} #{conv[ndx-1]}"
end
end
ndx = 7
return "#{'%.3f' % (size/(scale**(ndx-1)))} #{conv[ndx-1]}"
end
dir_path = "/Users/sagarbommidi/workspace"
Find.find(dir_path).each do |file|
if FileTest.file?(file) && File.extname(file) == '.log'
fsize = FileTest.size(file)
puts "#{file} ==> #{format_mb(fsize)} "
puts "You Want to delete file?"
decision = gets.chomp
if ["yes", 'y'].include? decision
puts "Deleting the file"
File.delete(file)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment