Last active
February 28, 2025 17:25
-
-
Save sagarbangade/75dd6d823405181a64a5ee79adeecdda to your computer and use it in GitHub Desktop.
Bash Script: File Content Extractor with Exclusion Support This Bash script scans the current directory and its subdirectories to list and extract file contents while excluding specified files and folders. It is useful for generating a text-based snapshot of a project’s structure and content.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Output file name | |
output_file="output.txt" | |
# Remove the output file if it already exists | |
rm -f "$output_file" | |
# Array of directories and files to exclude | |
exclude_patterns=( | |
"./node_modules" | |
"./.git" | |
"./.gitignore" | |
"./codecopy.sh" | |
"./package-lock.json" | |
"./output.txt" | |
) | |
# Build the find command with exclusions | |
find . -type d \( -name "node_modules" -o -name ".git" \) -prune -o \ | |
-type f \( ! -name ".gitignore" ! -name "codecopy.sh" ! -name "package-lock.json" ! -name "output.txt" \) -print0 | | |
while IFS= read -r -d $'\0' file; do | |
# Get the directory path relative to the script's location | |
file_dir=$(dirname "$file") | |
file_name=$(basename "$file") | |
# Determine the location path | |
if [ "$file_dir" = "." ]; then | |
location="/" | |
else | |
location="$file_dir/" | |
fi | |
# Append information to the output file | |
echo "// location of file and file name" >> "$output_file" | |
echo "// ${location}${file_name}" >> "$output_file" | |
echo "" >> "$output_file" # Add a newline for better formatting | |
# Output content of the file | |
cat "$file" >> "$output_file" | |
echo "" >> "$output_file" # Add a newline to separate file contents | |
echo "-----------------------------------------" >> "$output_file" # Separator between files | |
echo "" >> "$output_file" # Add a newline | |
done | |
echo "Script finished. Output written to '$output_file'" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment