all repos

tmux-stare @ 7984755ea09e213bcac207425e9fbcf99744f694

session manager, but my session manager

tmux-stare/prune.sh (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
add prune script, 16 hours ago
1
#!/usr/bin/env bash
2
CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
3
source "$CURRENT_DIR/helpers.sh"
4
5
set -euo pipefail
6
7
DAYS=7
8
9
DIR="$(get_opt_dir)"
10
cd "$DIR"
11
12
CUTOFF=$(date -d "$DAYS days ago" +%Y%m%dT%H%M%S)
13
14
# Build set of active session names (those with a _last symlink)
15
declare -A ACTIVE
16
for l in *_last; do
17
  [ -L "$l" ] || continue
18
  ACTIVE["${l%_last}"]=1
19
done
20
21
DELETED=0
22
FIXED=0
23
24
for f in *_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]T[0-9][0-9][0-9][0-9][0-9][0-9]; do
25
  [ -f "$f" ] || continue
26
  name="${f%_*}"
27
  ts="${f##*_}"
28
29
  if [[ -v ACTIVE["$name"] ]]; then
30
    # Active session: keep the _last target always, prune old others
31
    target="$(readlink "$name"_last 2>/dev/null || true)"
32
    if [ "$f" = "$target" ]; then continue; fi  # always keep the _last target
33
    if [[ "$ts" < "$CUTOFF" ]]; then
34
      rm "$f"
35
      echo "pruned: $f"
36
      DELETED=$((DELETED + 1))
37
    fi
38
  else
39
    # Orphaned session: delete everything
40
    rm "$f"
41
    echo "pruned (orphan): $f"
42
    DELETED=$((DELETED + 1))
43
  fi
44
done
45
46
# Clean dangling _last symlinks (target was deleted)
47
for l in *_last; do
48
  [ -L "$l" ] || continue
49
  [ -e "$l" ] && continue
50
  rm "$l"
51
  echo "cleaned: $l (broken symlink)"
52
  FIXED=$((FIXED + 1))
53
done
54
55
# Clean dangling global last symlink
56
if [ -L "last" ] && [ ! -e "last" ]; then
57
  rm "last"
58
  echo "cleaned: last (broken symlink)"
59
  FIXED=$((FIXED + 1))
60
fi
61
62
echo "Done. $DELETED session files pruned, $FIXED broken symlinks cleaned."