#!/usr/bin/env bash set -euo pipefail usage() { echo "Использование: $0 --dir <папка> [--apply]" } root="" apply=0 while [[ $# -gt 0 ]]; do case "$1" in --dir) root="${2:-}"; shift 2 ;; --apply) apply=1; shift ;; -h|--help) usage; exit 0 ;; *) echo "Неизвестный аргумент: $1" >&2; usage >&2; exit 2 ;; esac done if [[ -z "$root" || ! -d "$root" ]]; then echo "Укажите существующую папку через --dir" >&2 exit 2 fi count=0 while :; do mapfile -d '' -t dirs < <(find "$root" -depth -type d -empty -print0) removed=0 for directory in "${dirs[@]}"; do if [[ "$directory" == "$root" ]]; then continue fi if [[ "$apply" -eq 0 ]]; then echo "Пустая папка: $directory" count=$((count + 1)) elif rmdir -- "$directory" 2>/dev/null; then echo "Удалена: $directory" removed=$((removed + 1)) count=$((count + 1)) fi done if [[ "$apply" -eq 0 || "$removed" -eq 0 ]]; then break fi done if [[ "$count" -eq 0 ]]; then echo "Пустых папок не найдено." elif [[ "$apply" -eq 0 ]]; then echo "Найдено: $count. Это предпросмотр, для удаления добавьте --apply." else echo "Удалено папок: $count" fi