#!/usr/bin/env bash set -euo pipefail usage() { echo "Использование: $0 --dir <папка> --out <папка> [--move] [--dry-run]" } die() { echo "Ошибка: $*" >&2 exit 1 } dir="" out="" move=0 dry_run=0 while [[ $# -gt 0 ]]; do case "$1" in --dir) dir="${2:-}"; shift 2 ;; --out) out="${2:-}"; shift 2 ;; --move) move=1; shift ;; --dry-run) dry_run=1; shift ;; -h|--help) usage; exit 0 ;; *) echo "Неизвестный аргумент: $1" >&2; usage >&2; exit 2 ;; esac done [[ -n "$dir" && -n "$out" ]] || { usage >&2; exit 2; } [[ -d "$dir" ]] || die "папка не найдена: $dir" mkdir -p "$out" shopt -s nullglob nocasematch done_count=0 skipped=0 planned=0 while IFS= read -r -d '' file; do name="${file##*/}" case "$name" in *screenshot*|*screen\ shot*|*snapshot*|*снимок*|*Снимок*|*скриншот*|*Скриншот*) ;; *) continue ;; esac extension="${name##*.}" extension="${extension,,}" case "$extension" in jpg|jpeg|png|webp|heic|gif) ;; *) continue ;; esac year_month=$(date -r "$file" +%Y/%m) destination="$out/$year_month" target="$destination/$name" if [[ -e "$target" ]]; then stem="${name%.*}" suffix="${name##*.}" counter=1 while [[ -e "$destination/${stem}-${counter}.${suffix}" ]]; do counter=$((counter + 1)) done target="$destination/${stem}-${counter}.${suffix}" skipped=$((skipped + 1)) fi if [[ "$dry_run" -eq 1 ]]; then echo "[dry-run] $file -> $target" planned=$((planned + 1)) continue fi mkdir -p "$destination" if [[ "$move" -eq 1 ]]; then mv -- "$file" "$target" echo "Перемещено: $file -> $target" else cp -p -- "$file" "$target" echo "Скопировано: $file -> $target" fi done_count=$((done_count + 1)) done < <(find "$dir" -maxdepth 1 -type f -print0) action="скопировано" if [[ "$move" -eq 1 ]]; then action="перемещено" fi if [[ "$dry_run" -eq 1 ]]; then echo "Итог: $planned запланировано, конфликтов имен $skipped" else echo "Итог: $done_count $action, конфликтов имен $skipped" fi