#!/usr/bin/env bash shopt -s nullglob usage() { echo "Usage: $(basename "$0") [ -x ]*" exit 1 } remove_empty_directories() { find . -depth -type d | sed 's|^\./||' | grep -Ev '^\.$|^\.git/|^\.git$' | while read -r d; do for f in "$d"/{*,.*}; do if ! [ -e "$f" ]; then rmdir "$d" fi break done done } git_options=(-e TAGS) while [ "${1#-}" != "$1" ]; do case "$1" in -x) git_options+=(-e "$2") shift; shift;; *) usage;; esac done for d in "${@-.}"; do ( if cd "$d"; then if [ -e .git ] || [ -e ../.git ]; then if ! type -p git >&/dev/null; then echo "$0: git: not found." exit 0 fi git clean -f -d -x "${git_options[@]}" >/dev/null remove_empty_directories else echo "$0: $d: not administered by Git." fi fi ) done