#! /bin/bash
# Last edited on 2025-08-16 07:03:46 by stolfi

# Find and remove empty subdirs of the current directory,
# as well as subdirs that are made empty by this command.

# Up to 10 levels deep: 
for n in 0 1 2 3 4 5 6 7 8 9 ; do
  find ./ -name .cache -prune -o -name .snap -prune -o -type d -empty -print \
    | sed -e "s:^:rmdir -v ':g" -e "s:$:':g" \
    > .empties
  wc -l .empties 1>&2
  if [[ -s .empties ]]; then
    source .empties
  else
    break
  fi
done
if [[ ! ( -s .empties ) ]]; then rm -fv .empties; fi
