Skip to content

Instantly share code, notes, and snippets.

@mahdi-malv
Created June 6, 2025 08:50
Show Gist options
  • Save mahdi-malv/a06ad2c6d05684999056bbf2f1e43503 to your computer and use it in GitHub Desktop.
Save mahdi-malv/a06ad2c6d05684999056bbf2f1e43503 to your computer and use it in GitHub Desktop.
Extract all source codes (filter by file ext) into one txt file for LLM context
#!/bin/bash
print_usage() {
echo "Usage: $0 -i <input_directory> -o <output_directory> -x <extensions>"
echo "Example: $0 -i input_dir -o output_dir -x kt java xml"
}
while getopts "i:o:x:" opt; do
case $opt in
i) input_directory="$OPTARG" ;;
o) output_directory="$OPTARG" ;;
x) extensions="$OPTARG" ;;
\?) print_usage
exit 1 ;;
esac
done
if [ -z "$input_directory" ] || [ -z "$output_directory" ] || [ -z "$extensions" ]; then
print_usage
exit 1
fi
mkdir -p "$output_directory"
output_file="$output_directory/merged.txt"
> "$output_file"
IFS=' ' read -r -a ext_array <<< "$extensions"
for ext in "${ext_array[@]}"; do
find "$input_directory" -type f -name "*.$ext" | while read -r file; do
echo -e "\n// file: $(basename "$file")\n" >> "$output_file"
cat "$file" >> "$output_file"
done
done
echo "All merged into $output_file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment