Files
skill-repo/skills/obsidian-vault-kb/scripts/vault_backlinks.sh
T
Henner M. Kruse c774df7ad7 Initial commit
2026-08-04 00:18:16 +02:00

32 lines
1.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# Find backlinks (notes containing [[note-name]]) within a configured vault.
#
# Usage: vault_backlinks.sh <note-name> [vault-name]
#
# The vault path always comes from
# ~/.agent-skills/obsidian-vault-kb/config.json — never from a raw argument.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=_lib.sh
source "$SCRIPT_DIR/_lib.sh"
NOTE_NAME="${1:?Usage: vault_backlinks.sh <note-name> [vault-name]}"
VAULT_NAME="${2:-}"
VAULT_RAW="$(resolve_vault_path "$VAULT_NAME")"
VAULT="$(canonicalize_and_check_dir "$VAULT_RAW")"
# Build a regex-safe literal for the note name (escape regex metachars).
ESCAPED_NAME=$(printf '%s' "$NOTE_NAME" | sed -e 's/[.[\*^$/]/\\&/g')
echo "== Notes linking to [[$NOTE_NAME]] in $VAULT =="
echo
if command -v rg >/dev/null 2>&1; then
rg -n "\[\[${ESCAPED_NAME}(\||\]\])" "$VAULT" \
-g '!.obsidian' -g '!.git' -g '*.md' || echo "(no backlinks found)"
else
grep -rn "\[\[${ESCAPED_NAME}" "$VAULT" --include='*.md' 2>/dev/null || echo "(no backlinks found)"
fi