Skip to content

Instantly share code, notes, and snippets.

@lamngockhuong
Created May 13, 2025 07:22
Show Gist options
  • Save lamngockhuong/7c5ba9565b660c723aa36b483a408443 to your computer and use it in GitHub Desktop.
Save lamngockhuong/7c5ba9565b660c723aa36b483a408443 to your computer and use it in GitHub Desktop.
A handy interactive Bash script to inspect and optionally clean up disk space used by common development tools and system caches on Ubuntu (or any Linux system).
#!/bin/bash
GREEN="\e[32m"
RED="\e[31m"
YELLOW="\e[33m"
CYAN="\e[36m"
NC="\e[0m"
echo -e "${YELLOW}πŸ“¦ Checking disk usage of common development folders...${NC}"
echo
FOLDERS=(
"$HOME/.npm"
"$HOME/.yarn"
"$HOME/.bun"
"$HOME/.nvm"
"$HOME/.sdkman"
"$HOME/.gradle"
"$HOME/.m2"
"$HOME/.vscode"
"$HOME/.eclipse"
"$HOME/.docker"
"$HOME/snap"
)
for FOLDER in "${FOLDERS[@]}"; do
if [ -d "$FOLDER" ]; then
SIZE=$(du -sh "$FOLDER" 2>/dev/null | cut -f1)
echo -e "${CYAN}$FOLDER${NC} β†’ ${GREEN}$SIZE${NC}"
echo -en "${YELLOW}Do you want to delete this folder? (y/N): ${NC}"
read -r CONFIRM
if [[ "$CONFIRM" =~ ^[Yy]$ ]]; then
rm -rf "$FOLDER"
echo -e "${RED}Deleted $FOLDER${NC}"
else
echo -e "${CYAN}Skipped $FOLDER${NC}"
fi
echo
fi
done
echo -e "${YELLOW}🧾 Checking system-level caches...${NC}"
echo
# APT cache
if [ -d "/var/cache/apt/archives" ]; then
APT_SIZE=$(sudo du -sh /var/cache/apt/archives | cut -f1)
echo -e "${CYAN}APT cache${NC} β†’ ${GREEN}$APT_SIZE${NC}"
echo -en "${YELLOW}Clean APT cache? (y/N): ${NC}"
read -r CLEAN_APT
if [[ "$CLEAN_APT" =~ ^[Yy]$ ]]; then
echo -e "${GREEN}β†’ Cleaning APT...${NC}"
sudo apt autoremove -y && sudo apt clean
else
echo -e "${CYAN}Skipped APT cleanup.${NC}"
fi
echo
fi
# Journal logs
if [ -d "/var/log/journal" ]; then
JOURNAL_SIZE=$(sudo du -sh /var/log/journal | cut -f1)
echo -e "${CYAN}Journal logs${NC} β†’ ${GREEN}$JOURNAL_SIZE${NC}"
echo -en "${YELLOW}Vacuum journal logs (older than 7 days)? (y/N): ${NC}"
read -r CLEAN_JOURNAL
if [[ "$CLEAN_JOURNAL" =~ ^[Yy]$ ]]; then
echo -e "${GREEN}β†’ Cleaning journal logs...${NC}"
sudo journalctl --vacuum-time=7d
else
echo -e "${CYAN}Skipped journal logs cleanup.${NC}"
fi
echo
fi
# Docker system
if command -v docker &> /dev/null; then
echo -e "${CYAN}Docker disk usage:${NC}"
docker system df
echo -en "${YELLOW}Prune Docker system (remove unused data)? (y/N): ${NC}"
read -r CLEAN_DOCKER
if [[ "$CLEAN_DOCKER" =~ ^[Yy]$ ]]; then
echo -e "${GREEN}β†’ Cleaning Docker system...${NC}"
docker system prune -a -f
else
echo -e "${CYAN}Skipped Docker cleanup.${NC}"
fi
echo
fi
echo -e "${GREEN}βœ… Done. Disk cleanup process finished.${NC}"
@lamngockhuong
Copy link
Author

lamngockhuong commented May 13, 2025

🧹 Script: check-disk-usage.sh

A handy interactive Bash script to inspect and optionally clean up disk space used by common development tools and system caches on Ubuntu (or any Linux system).

πŸ” What it does:

  • Scans common dev tool folders (e.g. .npm, .yarn, .m2, .gradle, .nvm, snap, etc.) and shows their sizes.

  • Prompts for deletion of each folder individually.

  • Reports disk usage of:

    • APT package cache
    • journalctl system logs
    • Docker system (images, volumes, containers)
  • Prompts separately before cleaning:

    • APT cache (sudo apt autoremove && apt clean)
    • Systemd journal logs (journalctl --vacuum-time=7d)
    • Docker unused data (docker system prune -a)

βœ… Features:

  • Simple interactive prompts for safe manual control
  • Works well on local dev machines or CI runners with disk pressure
  • Requires no dependencies besides standard Linux tools

πŸš€ Usage

Download and run:

chmod +x check-disk-usage.sh
./check-disk-usage.sh

Or run directly from Gist in one line:

bash <(curl -L https://go.khuong.dev/check-disk-usage)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment