Skip to content

Instantly share code, notes, and snippets.

@randomecho
Last active January 18, 2016 03:14
Show Gist options
  • Save randomecho/901aae0ceb3492f128a1 to your computer and use it in GitHub Desktop.
Save randomecho/901aae0ceb3492f128a1 to your computer and use it in GitHub Desktop.
Delete empty directories recursively
#!/usr/bin/env ruby
#
# Delete empty directories recursively
#
# Author:: Soon Van - randomecho.com
# Copyright:: Copyright 2016 Soon Van
# License:: http://opensource.org/licenses/BSD-3-Clause
require 'FileUtils'
@total_removed = 0
def purge_empties(path_to)
Dir.foreach(path_to) do |dir|
full_path = path_to + dir
if dir[0,1] != '.' && File.directory?(full_path)
if Dir.entries(full_path) == ['.', '..']
FileUtils.rm_rf(full_path)
puts "Deleted: #{full_path}"
@total_removed += 1
else
purge_empties(full_path + "/")
end
end
end
return
end
#Gem.win_platform? ? (system "cls") : (system "clear")
if ARGV[0].nil?
puts "! No folder specified."
puts "Use the following format: \n\n"
puts "$ #{File.basename(__FILE__)} /path/to"
exit
else
purge_empties(ARGV[0])
if @total_removed > 0
puts "Total removed: #{@total_removed}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment