Skip to content

Instantly share code, notes, and snippets.

@samrolken
Created November 26, 2023 19:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samrolken/f5e6061bfe7e560fcdcf232fe288e8f2 to your computer and use it in GitHub Desktop.
Save samrolken/f5e6061bfe7e560fcdcf232fe288e8f2 to your computer and use it in GitHub Desktop.
This is a little script I wrote for my own use. It gets source code files from the current directory and includes them in the output, filtering out huge source code files and sorting them by last modified. The idea is that this should grab the source code content for the code I'm working on up to a certain amount, so I can paste it into ChatGPT.
#!/bin/bash
# Parameters with default values
max_size=${1:-25} # Max size in kilobytes
output_size=${2:-50} # Max output size in kilobytes
# Extensions array - easy to add more extensions
declare -a extensions=("*.c" "*.cpp" "*.rb" "*.js")
# Temporary file to store intermediate results
temp_file=$(mktemp)
# Find and sort files
for ext in "${extensions[@]}"; do
find . -name "$ext" -size -${max_size}k -type f -print0 |
xargs -0 ls -lt |
awk '{print $9}' >> "$temp_file"
done
# Initialize output size counter
current_output_size=0
# Process files and generate markdown
while IFS= read -r file; do
# Get file size in kilobytes
file_size=$(du -k "$file" | cut -f1)
new_output_size=$((current_output_size + file_size))
# Check if adding this file would exceed the output size limit
if [ "$new_output_size" -le "$output_size" ]; then
# Add file path as header
echo "### $file"
echo '```'
cat "$file" # Output the content of the file
echo '```'
echo
# Update the output size counter
current_output_size=$new_output_size
else
break
fi
done < "$temp_file"
# Clean up
rm "$temp_file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment