1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/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