1. #!/bin/bash
  2.  
  3. SEARCH_DIR="/mnt/your-partition"
  4. OUTPUT_DIR="/mnt/your-partition/results"
  5. mkdir -p "$OUTPUT_DIR"
  6.  
  7. terms=()
  8.  
  9. echo "=== RG Search Tool ==="
  10. echo "Search directory: $SEARCH_DIR"
  11. echo "Tip: Use + to combine terms (e.g. football+poker matches lines containing BOTH)"
  12. echo ""
  13.  
  14. while true; do
  15. read -p "Enter search term (or term1+term2 for combo): " term
  16. terms+=("$term")
  17.  
  18. while true; do
  19. echo ""
  20. echo "Current terms: ${terms[*]}"
  21. echo "1) Add another term"
  22. echo "2) Start searching"
  23. read -p "Choice: " choice
  24.  
  25. if [[ "$choice" == "1" ]]; then
  26. break
  27. elif [[ "$choice" == "2" ]]; then
  28. break 2
  29. else
  30. echo "Invalid choice, enter 1 or 2"
  31. fi
  32. done
  33. done
  34.  
  35. echo ""
  36. echo "=== Searching all terms in one pass... ==="
  37.  
  38. # Build pattern - handle + combos as lookaheads so both must appear in any order
  39. build_pattern() {
  40. local term="$1"
  41. if [[ "$term" == *"+"* ]]; then
  42. IFS='+' read -ra parts <<< "$term"
  43. local pattern=""
  44. for part in "${parts[@]}"; do
  45. pattern+="(?=.*${part})"
  46. done
  47. echo "${pattern}.*"
  48. else
  49. echo "$term"
  50. fi
  51. }
  52.  
  53. # Run one rg pass per unique pattern (combos cant be merged into one pipe easily)
  54. for term in "${terms[@]}"; do
  55. clean_name="${term//[^a-zA-Z0-9]/_}"
  56. output_file="$OUTPUT_DIR/${clean_name}.txt"
  57. pattern=$(build_pattern "$term")
  58.  
  59. echo "Searching: $term (pattern: $pattern)"
  60. rg -iP "$pattern" "$SEARCH_DIR" --no-filename --no-line-number | sort -u > "$output_file"
  61.  
  62. count=$(wc -l < "$output_file")
  63. echo " -> Found $count results -> ${clean_name}.txt"
  64. done
  65.  
  66. echo ""
  67. echo "=== Done! Results saved to $OUTPUT_DIR ==="