Skip to content

Instantly share code, notes, and snippets.

@kakilangit
Created December 17, 2025 09:21
Show Gist options
  • Select an option

  • Save kakilangit/80339ba68eff86eea0b2ca5904c266bb to your computer and use it in GitHub Desktop.

Select an option

Save kakilangit/80339ba68eff86eea0b2ca5904c266bb to your computer and use it in GitHub Desktop.
Script to find and clean all Rust projects in a given directory
#!/bin/bash
# Script to find and clean all Rust projects in a given directory
# Usage: ./cargo-clean-all.sh [directory]
# Example: ./cargo-clean-all.sh ~/dev/
set -e
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Default to current directory if no argument provided
SEARCH_DIR="${1:-.}"
# Expand tilde to home directory
SEARCH_DIR="${SEARCH_DIR/#\~/$HOME}"
# Check if directory exists
if [ ! -d "$SEARCH_DIR" ]; then
echo -e "${RED}Error: Directory '$SEARCH_DIR' does not exist${NC}"
exit 1
fi
# Check if cargo is installed
if ! command -v cargo &> /dev/null; then
echo -e "${RED}Error: cargo is not installed or not in PATH${NC}"
exit 1
fi
# Check if ripgrep is installed, fallback to find if not available
if command -v rg &> /dev/null; then
SEARCH_TOOL="rg"
else
SEARCH_TOOL="find"
echo -e "${YELLOW}Note: ripgrep not found, using find (slower). Install ripgrep for faster searches.${NC}"
fi
echo -e "${BLUE}Searching for Rust projects in: $SEARCH_DIR${NC}"
echo -ne "${YELLOW}Scanning directories...${NC}"
# Find all directories containing Cargo.toml
if [ "$SEARCH_TOOL" = "rg" ]; then
RUST_PROJECTS=$(rg --files --glob "Cargo.toml" "$SEARCH_DIR" 2>/dev/null | sed 's|/Cargo.toml$||')
else
RUST_PROJECTS=$(find "$SEARCH_DIR" -name "Cargo.toml" -type f 2>/dev/null | sed 's|/Cargo.toml$||')
fi
echo -e "\r${GREEN}✓ Scan complete!${NC} "
echo ""
# Count projects found
PROJECT_COUNT=$(echo "$RUST_PROJECTS" | grep -c . || echo "0")
if [ "$PROJECT_COUNT" -eq 0 ]; then
echo -e "${YELLOW}No Rust projects found in '$SEARCH_DIR'${NC}"
exit 0
fi
echo -e "${GREEN}Found $PROJECT_COUNT Rust project(s)${NC}"
echo ""
# Track statistics
CLEANED=0
FAILED=0
TOTAL_SIZE_BEFORE=0
TOTAL_SIZE_AFTER=0
# Clean each project
while IFS= read -r project_dir; do
if [ -z "$project_dir" ]; then
continue
fi
echo -e "${BLUE}Processing: $project_dir${NC}"
# Calculate target directory size before cleaning (if it exists)
TARGET_DIR="$project_dir/target"
SIZE_BEFORE=0
if [ -d "$TARGET_DIR" ]; then
SIZE_BEFORE=$(du -sk "$TARGET_DIR" 2>/dev/null | cut -f1 || echo "0")
TOTAL_SIZE_BEFORE=$((TOTAL_SIZE_BEFORE + SIZE_BEFORE))
echo -e " Target size: $(numfmt --to=iec-i --suffix=B $((SIZE_BEFORE * 1024)) 2>/dev/null || echo "${SIZE_BEFORE}KB")"
else
echo -e " ${YELLOW}No target directory found (already clean)${NC}"
fi
# Run cargo clean
if (cd "$project_dir" && cargo clean 2>&1); then
echo -e " ${GREEN}✓ Cleaned successfully${NC}"
CLEANED=$((CLEANED + 1))
else
echo -e " ${RED}✗ Failed to clean${NC}"
FAILED=$((FAILED + 1))
fi
# Calculate size after cleaning
SIZE_AFTER=0
if [ -d "$TARGET_DIR" ]; then
SIZE_AFTER=$(du -sk "$TARGET_DIR" 2>/dev/null | cut -f1 || echo "0")
TOTAL_SIZE_AFTER=$((TOTAL_SIZE_AFTER + SIZE_AFTER))
fi
echo ""
done <<< "$RUST_PROJECTS"
# Print summary
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}Summary:${NC}"
echo -e " Total projects found: $PROJECT_COUNT"
echo -e " ${GREEN}Successfully cleaned: $CLEANED${NC}"
if [ "$FAILED" -gt 0 ]; then
echo -e " ${RED}Failed: $FAILED${NC}"
fi
# Calculate space freed
SPACE_FREED=$((TOTAL_SIZE_BEFORE - TOTAL_SIZE_AFTER))
if [ "$SPACE_FREED" -gt 0 ]; then
SPACE_FREED_HUMAN=$(numfmt --to=iec-i --suffix=B $((SPACE_FREED * 1024)) 2>/dev/null || echo "${SPACE_FREED}KB")
echo -e " ${GREEN}Space freed: $SPACE_FREED_HUMAN${NC}"
fi
echo -e "${BLUE}========================================${NC}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment