#!/bin/bash
SEARCH_DIR="/mnt/your-partition"
OUTPUT_DIR="/mnt/your-partition/results"
mkdir -p "$OUTPUT_DIR"
terms=()
echo "=== RG Search Tool ==="
echo "Search directory: $SEARCH_DIR"
echo "Tip: Use + to combine terms (e.g. football+poker matches lines containing BOTH)"
echo ""
while true; do
read -p "Enter search term (or term1+term2 for combo): " term
terms+=("$term")
while true; do
echo ""
echo "Current terms: ${terms[*]}"
echo "1) Add another term"
echo "2) Start searching"
read -p "Choice: " choice
if [[ "$choice" == "1" ]]; then
break
elif [[ "$choice" == "2" ]]; then
break 2
else
echo "Invalid choice, enter 1 or 2"
fi
done
done
echo ""
echo "=== Searching all terms in one pass... ==="
# Build pattern - handle + combos as lookaheads so both must appear in any order
build_pattern() {
local term="$1"
if [[ "$term" == *"+"* ]]; then
IFS='+' read -ra parts <<< "$term"
local pattern=""
for part in "${parts[@]}"; do
pattern+="(?=.*${part})"
done
echo "${pattern}.*"
else
echo "$term"
fi
}
# Run one rg pass per unique pattern (combos cant be merged into one pipe easily)
for term in "${terms[@]}"; do
clean_name="${term//[^a-zA-Z0-9]/_}"
output_file="$OUTPUT_DIR/${clean_name}.txt"
pattern=$(build_pattern "$term")
echo "Searching: $term (pattern: $pattern)"
rg -iP "$pattern" "$SEARCH_DIR" --no-filename --no-line-number | sort -u > "$output_file"
count=$(wc -l < "$output_file")
echo " -> Found $count results -> ${clean_name}.txt"
done
echo ""
echo "=== Done! Results saved to $OUTPUT_DIR ==="