Skip to content

Instantly share code, notes, and snippets.

@ccorcos
Created January 27, 2026 18:11
Show Gist options
  • Select an option

  • Save ccorcos/163800f5065fa98aed14554e6e73882b to your computer and use it in GitHub Desktop.

Select an option

Save ccorcos/163800f5065fa98aed14554e6e73882b to your computer and use it in GitHub Desktop.
Turn a repository into a flat markdown file
#!/bin/bash
# flat.sh - Concatenate files into flat.md with filenames as headers
output="flat.md"
> "$output" # Clear/create output file
process_file() {
local file="$1"
if [[ -f "$file" ]]; then
echo "## $file" >> "$output"
echo '```' >> "$output"
cat "$file" >> "$output"
echo '```' >> "$output"
echo "" >> "$output"
fi
}
process_path() {
local path="$1"
if [[ -f "$path" ]]; then
process_file "$path"
elif [[ -d "$path" ]]; then
while IFS= read -r -d '' file; do
process_file "$file"
done < <(find "$path" -type f -print0 | sort -z)
else
echo "Warning: '$path' not found" >&2
fi
}
for arg in "$@"; do
process_path "$arg"
done
echo "Created $output"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment