testsAndMisc/linux_configuration/utils/find_keepassxc.sh
Krzysztof kuhy Rudnicki 4f2b0ccdf7 Dissolve linux_configuration/scripts/ and single_use/
Two levels of pure taxonomy carrying no information, and a 2-directory
cap leaves no room for them. Categories now sit directly under
linux_configuration/: features, fixes, utils, misc, fresh-install,
installers, diagnostics, gaming, boot_recovery, periodic_background, lib,
meta, tests. A script's own lib/ and tests/ are exempt from the cap, so
those stay put.

Depth violations 248 -> 139; roughly 40 of the remainder are inside
digital_wellbeing and hosts, deferred until the guard-lib repoint.

The moves are the easy half. What breaks is every path that counted
levels, and three classes of it had to be found by resolving paths rather
than reading diffs:

  - 52 relative `source` lines for lib/common.sh and lib/android.sh. The
    correct prefix differs per file: 40 were ../../ and 12 were ../../../,
    so a blanket replace would have fixed one set and broken the other.
  - REPO_ROOT in 7 fixes/lib/tests harnesses and setup_syncyomi_guard.sh,
    each walking up four levels from a directory now two levels shallower.
    This caused a real regression -- the fixes suite went red with
    "log_warn: command not found" -- caught by running it and comparing
    against the pre-move baseline worktree.
  - conftest.py's sys.path entries, which took the pytest gate down with
    14 collection errors, and the `# shellcheck source=` directives, which
    took shellcheck down with SC1091.

16 symlinks in $HOME broke, including the entire i3blocks bar. All
repointed, and cpu_monitor/memory/disk/time/volume were run to confirm
they emit real output rather than merely resolving.

Verified: 1798 tests pass at 100% coverage, 8 of 9 lib/tests suites pass
(the ninth needs the coverage jail, identically at baseline), pre-commit
is clean, and the two untracked secret configs are still ignored after
their gitignore paths changed.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PJrqKERAo5QUeEHWS5Asgp
2026-08-23 18:53:30 +02:00

125 lines
3.5 KiB
Bash
Executable File

#!/bin/bash
# Find all KeePassXC database files (.kdbx) on the system and move them to a single location
# Uses 'fd' for fast searching - install with: sudo pacman -S fd
#
# Usage: ./find_keepassxc.sh [destination_directory]
# Default destination: ~/Keepass
set -euo pipefail
# Source common library if available
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
if [[ -f "$SCRIPT_DIR/../lib/common.sh" ]]; then
# shellcheck source=../lib/common.sh
source "$SCRIPT_DIR/../lib/common.sh"
else
log() { printf '[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$*"; }
fi
# Configuration
DEST_DIR="${1:-$HOME/Keepass}"
SEARCH_ROOT="/"
TIMEOUT_SECONDS=30
# Ensure fd is installed
if ! command -v fd &> /dev/null; then
log "ERROR: 'fd' is not installed. Install with: sudo pacman -S fd"
exit 1
fi
# Create destination directory if it doesn't exist
mkdir -p "$DEST_DIR"
log "Destination directory: $DEST_DIR"
# Find all .kdbx files using fd (very fast, respects .gitignore by default, but we use -u for unrestricted)
# -e kdbx: search by extension
# -u: unrestricted (include hidden and ignored files)
# -a: absolute paths
# --one-file-system: don't cross filesystem boundaries (optional, remove if you want to search mounted drives)
log "Searching for .kdbx files across the system (timeout: ${TIMEOUT_SECONDS}s)..."
# Use timeout to ensure the search doesn't take too long
# Exclude /proc, /sys, /dev, /run, /tmp, /var/cache, /var/tmp for speed
FOUND_FILES=$(timeout "$TIMEOUT_SECONDS" fd \
-e kdbx \
-u \
-a \
--exclude '/proc' \
--exclude '/sys' \
--exclude '/dev' \
--exclude '/run' \
--exclude '/tmp' \
--exclude '/var/cache' \
--exclude '/var/tmp' \
--exclude '/snap' \
--exclude '/.snapshots' \
--exclude '/lost+found' \
. "$SEARCH_ROOT" 2> /dev/null || true)
if [[ -z $FOUND_FILES ]]; then
log "No .kdbx files found."
exit 0
fi
# Count and display found files
FILE_COUNT=$(echo "$FOUND_FILES" | wc -l)
log "Found $FILE_COUNT .kdbx file(s):"
echo "$FOUND_FILES" | while read -r file; do
echo " - $file"
done
# Move files to destination
log "Moving files to $DEST_DIR..."
MOVED_COUNT=0
SKIPPED_COUNT=0
while IFS= read -r src_file; do
[[ -z $src_file ]] && continue
# Skip if file is already in destination
if [[ "$(dirname "$src_file")" == "$DEST_DIR" ]]; then
log "Skipping (already in destination): $src_file"
((SKIPPED_COUNT++)) || true
continue
fi
# Get the base filename
base_name=$(basename "$src_file")
dest_file="$DEST_DIR/$base_name"
# Handle filename conflicts by adding a number suffix
if [[ -f $dest_file ]]; then
# Check if it's the exact same file (by content)
if cmp -s "$src_file" "$dest_file"; then
log "Skipping (identical file exists): $src_file"
# Remove the duplicate source file
rm -v "$src_file"
((SKIPPED_COUNT++)) || true
continue
fi
# Different file with same name - add suffix
counter=1
name_without_ext="${base_name%.kdbx}"
while [[ -f $dest_file ]]; do
dest_file="$DEST_DIR/${name_without_ext} ($counter).kdbx"
((counter++))
done
log "Renaming to avoid conflict: $base_name -> $(basename "$dest_file")"
fi
# Move the file
if mv -v "$src_file" "$dest_file"; then
((MOVED_COUNT++)) || true
else
log "ERROR: Failed to move $src_file"
fi
done <<< "$FOUND_FILES"
log "Done! Moved $MOVED_COUNT file(s), skipped $SKIPPED_COUNT file(s)."
log "All KeePassXC databases are now in: $DEST_DIR"
# List final contents
log "Contents of $DEST_DIR:"
ls -la "$DEST_DIR"/*.kdbx 2> /dev/null || log "No .kdbx files in destination"