Skip to content

Instantly share code, notes, and snippets.

@ghall89
Created January 20, 2026 20:37
Show Gist options
  • Select an option

  • Save ghall89/2b9ab7ff8ab126ff46925c2a30d58cfc to your computer and use it in GitHub Desktop.

Select an option

Save ghall89/2b9ab7ff8ab126ff46925c2a30d58cfc to your computer and use it in GitHub Desktop.
# Recursively delete empty folders starting from the current directory
require 'find'
require 'fileutils'
root = '.'
empty_dirs = []
Find.find(root) do |path|
if File.directory?(path)
next if path == root
if Dir.entries(path).size <= 2
empty_dirs << path
end
end
end
empty_dirs.each do |dir|
print("Deleting #{dir}\n")
FileUtils.remove_dir(dir)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment