Skip to content

Instantly share code, notes, and snippets.

@krishvishal
Created April 12, 2025 21:03
Show Gist options
  • Save krishvishal/c6cfe05cdfe6cd6d801f38234f9a8aca to your computer and use it in GitHub Desktop.
Save krishvishal/c6cfe05cdfe6cd6d801f38234f9a8aca to your computer and use it in GitHub Desktop.
#!/bin/bash
# Usage: ./collect_sources.sh /path/to/dir rs output.txt
ROOT_DIR="$1"
EXT="$2"
OUTPUT="$3"
if [[ -z "$ROOT_DIR" || -z "$EXT" || -z "$OUTPUT" ]]; then
echo "Usage: $0 <root_dir> <extension_without_dot> <output_file>"
exit 1
fi
# Create or clear the output file
> "$OUTPUT"
# Add file tree to the output
echo "πŸ“ File Tree:" >> "$OUTPUT"
find "$ROOT_DIR" -type f -name "*.$EXT" | sed "s|$ROOT_DIR/||" | awk '{
n=split($0, parts, "/");
for (i=1; i<n; i++) printf "β”‚ ";
if (NR==1 || last_depth < n) printf "β”œβ”€β”€ ";
else printf "└── ";
print parts[n];
last_depth = n;
}' >> "$OUTPUT"
echo "" >> "$OUTPUT"
# Recursively add files with updated delimiters
find "$ROOT_DIR" -type f -name "*.$EXT" | while read -r file; do
echo "------ begin $file ------" >> "$OUTPUT"
cat "$file" >> "$OUTPUT"
echo "------ end $file ------" >> "$OUTPUT"
echo "" >> "$OUTPUT"
done
echo "βœ… Done. Output written to $OUTPUT"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment