Skip to content

Instantly share code, notes, and snippets.

@patrykgruszka
Created December 16, 2025 14:10
Show Gist options
  • Select an option

  • Save patrykgruszka/d99bc1e5ac5e32deb453963f9e8d7e9a to your computer and use it in GitHub Desktop.

Select an option

Save patrykgruszka/d99bc1e5ac5e32deb453963f9e8d7e9a to your computer and use it in GitHub Desktop.
bash script for creating context info for LLMs
#!/bin/bash
# --- Script Description ---
# Finds all text-based files in the current directory and its subdirectories,
# excluding dotfiles/dot-directories and specified filenames/directories,
# and prints their content in a Markdown-friendly format.
# --- Usage ---
# ./script_name.sh [-e file.txt] [-d node_modules] ...
# --- Initialization ---
# Array for file-based exclusions (uses -name)
FIND_EXCLUDE_FILES=(-not -name 'context.md')
# Array for path-based exclusions (uses -path for directories)
FIND_EXCLUDE_PATHS=()
# --- Option Parsing ---
# Use getopts to parse command-line options.
# -e: Exclude a file by its name.
# -d: Exclude a directory by its name/path.
while getopts ":e:d:" opt; do
case ${opt} in
e)
# Exclude based on filename.
FIND_EXCLUDE_FILES+=(-not -name "$OPTARG")
;;
d)
# Exclude based on path. The "*/.../*" pattern ensures we exclude
# any file within a directory of that name, anywhere in the tree.
# Example: -d 'node_modules' becomes -not -path '*/node_modules/*'
FIND_EXCLUDE_PATHS+=(-not -path "*/$OPTARG/*")
;;
\?)
echo "Invalid Option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
# --- Main Execution ---
# The find command now uses both arrays to build its exclusion list.
find . \
-type f \
-not -path '*/.*' \
"${FIND_EXCLUDE_FILES[@]}" \
"${FIND_EXCLUDE_PATHS[@]}" \
-exec sh -c '
# For each found file, check if it is a text-based or empty file.
if file "$1" | grep -qE "text|empty|JSON|XML|HTML"; then
# Print the file contents in a Markdown code block with a header.
echo "### File: $1"
echo "´´´"
cat "$1"
printf "\n´´´\n"
fi
' _ {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment