Skip to content

Instantly share code, notes, and snippets.

@goshki
Last active June 24, 2025 16:14
Show Gist options
  • Save goshki/9fed0392b92624b26c6adf4fea3df7e7 to your computer and use it in GitHub Desktop.
Save goshki/9fed0392b92624b26c6adf4fea3df7e7 to your computer and use it in GitHub Desktop.
Bash script to iterate over Unity projects in the current directory and clean them up (removes files and folders that Unity can regenerate when loading the project again)
#!/bin/bash
FOLDERS_TO_DELETE=("Library" "Temp" "obj" "Build" "Builds" "Logs")
FILES_TO_DELETE=("*.sln" "*.csproj")
DRY_RUN=true
if [[ "$1" == "--apply" ]]; then
DRY_RUN=false
fi
if [ "$DRY_RUN" = true ]; then
echo "--- Starting Unity projects clean-up in DRY RUN mode (use --apply to perform actual clean-up) ---"
echo "No files or folders will be deleted."
else
echo "--- Starting Unity projects clean-up in LIVE mode ---"
echo "WARNING: This script will delete files and folders immediately."
fi
echo "Current directory: $(pwd)"
echo ""
# Find all immediate subdirectories of the current directory
for project_dir in */; do
if [ -d "$project_dir" ]; then
echo ">>> Processing project: ${project_dir}"
for folder_name in "${FOLDERS_TO_DELETE[@]}"; do
TARGET_PATH="${project_dir}${folder_name}"
if [ -d "$TARGET_PATH" ]; then
if [ "$DRY_RUN" = true ]; then
echo " [DRY RUN] Would delete folder: $TARGET_PATH"
else
echo " Deleting folder: $TARGET_PATH"
rm -rf "$TARGET_PATH"
fi
fi
done
for file_pattern in "${FILES_TO_DELETE[@]}"; do
for file_to_delete in "${project_dir}"${file_pattern}; do
if [ -f "$file_to_delete" ]; then
if [ "$DRY_RUN" = true ]; then
echo " [DRY RUN] Would delete file: $file_to_delete"
else
echo " Deleting file: $file_to_delete"
rm -f "$file_to_delete"
fi
fi
done
done
fi
done
echo ""
if [ "$DRY_RUN" = true ]; then
echo "--- Dry Run Complete! ---"
else
echo "--- Cleanup Complete! ---"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment