#!/usr/bin/env bash # ============================================================== # Clean # -------------------------------------------------------------- # Author : Kris Yotam (aka. khr1st) # Contact : krisyotam@protonmail.com # License : GNU GPLv3 # Date : 2025-12-04 # -------------------------------------------------------------- # Description: # Interactive utility that walks directories under a given # Misc path and asks whether each file is "in use". Files # answered with 'n' (no) are queued for deletion and removed # at confirmation. This is intended to be invoked by `sort`. # ============================================================== set -eu MISC_BASE="${1:-$HOME/Misc}" if [ ! -d "$MISC_BASE" ]; then echo "Directory not found: $MISC_BASE" >&2 exit 1 fi declare -a TO_DELETE=() prompt_yes_no() { # prompt_yes_no "Question" -> returns 0 for yes, 1 for no printf "%s [Y/n]: " "$1" IFS= read -r ans || ans="" case "$ans" in [nN]) return 1 ;; "") return 0 ;; [yY]|[yY][eE][sS]) return 0 ;; *) return 1 ;; esac } echo "Cleaning: $MISC_BASE" for dir in "$MISC_BASE"/*; do [ -d "$dir" ] || continue echo "\nDirectory: $(basename "$dir")" for file in "$dir"/*; do [ -e "$file" ] || continue fname="$(basename "$file")" # Ask if file is being used. Default is Y (keep). printf "Is this file in use? %s\n" "$fname" if prompt_yes_no "Keep $fname?"; then echo "Keeping: $fname" else echo "Marked for deletion: $fname" TO_DELETE+=("$file") fi done done if [ ${#TO_DELETE[@]} -eq 0 ]; then echo "Nothing marked for deletion. Exiting." exit 0 fi echo "\nSummary: ${#TO_DELETE[@]} files marked for deletion." for f in "${TO_DELETE[@]}"; do echo " $(realpath --relative-to="$HOME" "$f")" done if prompt_yes_no "Delete these files now?"; then for f in "${TO_DELETE[@]}"; do rm -f -- "$f" && echo "Deleted: $(basename "$f")" done echo "Deletion complete." else echo "Aborted: no files were deleted." fi