dirty-helpers/rmdir-empty

27 lines
539 B
Text
Raw Permalink Normal View History

2022-05-28 14:02:01 +02:00
#!/bin/sh
# recursively delete empty directories
rmdir_empty() {
# make default . explicit
if [ -z "$1" ]; then
set -- .
fi
# do the first level directories first
for maxdepth_opt in "-maxdepth 1" ""; do
for d in "$@"; do
# directory might have been removed in the first level round
if [ -z "$max_depth_opt" -a ! -e "$d" ]; then
continue
fi
find "$d" -depth $maxdepth_opt \
-path "." -prune -or \
-type d -empty -exec rmdir -v {} \;
done
done
}
rmdir_empty "$@"