Created
May 13, 2025 07:22
-
-
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).
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 | |
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}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
π§Ή 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:
journalctl
system logsPrompts separately before cleaning:
sudo apt autoremove && apt clean
)journalctl --vacuum-time=7d
)docker system prune -a
)β Features:
π Usage
Download and run:
Or run directly from Gist in one line:
bash <(curl -L https://go.khuong.dev/check-disk-usage)