Skip to content

Instantly share code, notes, and snippets.

@agam
Created April 24, 2025 02:35
Show Gist options
  • Select an option

  • Save agam/8461aae511b77e3cec3f74f3f3067c16 to your computer and use it in GitHub Desktop.

Select an option

Save agam/8461aae511b77e3cec3f74f3f3067c16 to your computer and use it in GitHub Desktop.

Revisions

  1. agam created this gist Apr 24, 2025.
    57 changes: 57 additions & 0 deletions file_concat_with_names.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    #!/bin/bash

    # --- Configuration ---
    output_file="/Users/agam/tmp/combined_output.md"
    exclude_dir_names=(".venv" ".git" "node_modules" "__pycache__")
    # --- End Configuration ---

    # Optional: Delete the old output file first
    # rm -f "$output_file"

    echo "Starting file processing."
    echo "Output file: $output_file"
    echo "Excluding directories named: ${exclude_dir_names[@]}"
    echo "Excluding output file path: ./$output_file"

    # --- Build the find command ---
    find_cmd=("find" ".")

    # 1. Build the exclusion part
    prune_conditions=()
    prune_conditions+=("-path" "./$output_file")
    for dir_name in "${exclude_dir_names[@]}"; do
    prune_conditions+=("-o" "-name" "$dir_name" "-type" "d")
    done

    # Add the grouped prune conditions - Use literal '(' and ')' as arguments
    # CORRECTED LINE: Removed backslashes from \( and \)
    find_cmd+=("(" "${prune_conditions[@]}" ")" "-prune")

    # 2. Add the action part (what to do if NOT pruned)
    # CORRECTED LINE: Removed backslashes from \( and \)
    find_cmd+=("-o" "(" "-type" "f" "-print0" ")")

    # --- Execute the find command and process the results ---
    # Using process substitution for debugging the command if needed:
    # echo "Running command:" >&2
    # printf "'%s' " "${find_cmd[@]}" >&2
    # echo >&2

    "${find_cmd[@]}" | while IFS= read -r -d $'\0' file; do
    # Optional: Print progress to standard error (terminal)
    # echo "Processing: $file" >&2

    echo "$file" # Print the file path (to stdout -> goes to output file)
    echo '```' # Print the opening backticks
    cat "$file" # Print the file content
    echo # Ensure a newline exists before closing backticks
    echo '```' # Print the closing backticks
    echo # Optional: Add a blank line for separation
    done > "$output_file" # Redirect all loop stdout to the output file

    # Check find's exit status
    if [ $? -ne 0 ]; then
    echo "Warning: find command may have encountered errors." >&2
    fi

    echo "Processing finished. Output saved to: $output_file"