Skip to content

Instantly share code, notes, and snippets.

@kitingChris
Last active August 28, 2025 09:27
Show Gist options
  • Save kitingChris/370ca60d2b571e62e3b58513d2a0606c to your computer and use it in GitHub Desktop.
Save kitingChris/370ca60d2b571e62e3b58513d2a0606c to your computer and use it in GitHub Desktop.
recursivecat
#!/bin/bash
set -e
SCRIPT_NAME=$(basename "$0")
print_help() {
cat << EOF
$SCRIPT_NAME: Recursively print matching files with content and separators.
Usage:
$SCRIPT_NAME [directory] <filter1> [<filter2> ...]
Description:
- Recursively scans the specified directory (default: current directory).
- Filters files by one or more glob patterns (e.g., '*.sh', '*test*', '*.md').
- For each matched file, prints:
- The relative path
- A separator
- File content
- Another separator
Examples:
$SCRIPT_NAME . "*.sh" "*.md"
$SCRIPT_NAME "*.py"
$SCRIPT_NAME src "*main*"
Options:
-h, --help Show this help message and exit.
EOF
}
# --- Help option check ---
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
print_help
exit 0
fi
# --- Parse arguments ---
if [[ $# -lt 1 ]]; then
print_help
exit 1
fi
# If the first argument is a directory, use it; else use '.'
if [[ -d "$1" ]]; then
SCAN_DIR="$1"
shift
else
SCAN_DIR="."
fi
if [[ $# -eq 0 ]]; then
echo "Error: At least one filter required."
echo
print_help
exit 1
fi
# --- Build find command with filters ---
FIND_CMD=(find "$SCAN_DIR" -type f \( )
for filter in "$@"; do
FIND_CMD+=(-name "$filter" -o)
done
unset 'FIND_CMD[${#FIND_CMD[@]}-1]'
FIND_CMD+=(\))
# --- Scan and output ---
"${FIND_CMD[@]}" | while IFS= read -r file; do
echo "$file"
echo -n "-----------------------------"
cat "$file"
echo -n "-----------------------------"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment