#!/usr/bin/env bash set -euo pipefail usage() { echo "Использование: $0 --dir <папка> [--replacement <символ>] [--apply]" } dir="" replacement="-" apply=0 while [[ $# -gt 0 ]]; do case "$1" in --dir) dir="${2:-}"; shift 2 ;; --replacement) replacement="${2:-}"; shift 2 ;; --apply) apply=1; shift ;; -h|--help) usage; exit 0 ;; *) echo "Неизвестный аргумент: $1" >&2; usage >&2; exit 2 ;; esac done [[ -n "$dir" && -d "$dir" ]] || { echo "Укажите существующую папку через --dir" >&2; exit 2; } [[ -n "$replacement" && "$replacement" != */* && "$replacement" != "." && "$replacement" != ".." ]] || { echo "Недопустимая замена: $replacement" >&2 exit 2 } find "$dir" -depth -name '* *' -print0 | while IFS= read -r -d '' path; do parent="$(dirname "$path")" base="$(basename "$path")" new_name="${base// /$replacement}" while [[ "$new_name" == *"$replacement$replacement"* ]]; do new_name="${new_name//"$replacement$replacement"/$replacement}" done if [[ -z "$new_name" || "$new_name" == "." || "$new_name" == ".." ]]; then echo "Пропуск пустого имени: $path" >&2 continue fi destination="$parent/$new_name" if [[ "$destination" == "$path" ]]; then continue fi if [[ -e "$destination" ]]; then echo "Пропуск, уже существует: $destination" >&2 continue fi if [[ "$apply" -eq 0 ]]; then echo "Будет: $path -> $destination" else mv -- "$path" "$destination" echo "Готово: $destination" fi done if [[ "$apply" -eq 0 ]]; then echo "Это предпросмотр. Для применения добавьте --apply." fi