Disk usage analysis in linux
Disk usage analysis in linux
Most common approach if you have access to install something is ncdu command
sudo apt update && sudo apt install ncdu -y
Second approach:
sudo du -xh --max-depth=2 --exclude=/mnt / 2>/dev/null | sort -hr | head -n 20
with progress bar (not dynamic):
sudo du -xhd 2 --exclude=/mnt / 2>/dev/null | sort -rn | head -n 15 | awk '{printf "%-10s ", $1; max=50; bytes=$1; if(NR==1){max_val=bytes; if(max_val==0)max_val=1}; blocks=int((bytes/max_val)*max); bar=""; for(i=0;i<blocks;i++) bar=bar"#"; printf "%-50s %s\n", bar, $2}'
additional to exclude we can use
--exclude=/mnt --exclude=/media --exclude=/var/tmp
What to Look For (Common Targets)
When you read through your generated report, keep an eye out for these frequent culprits in those two specific directories:
In
/var:/var/log/journal/: Systemd logs. If this is massive, cap it to 1 GB max using:sudo journalctl --vacuum-size=1G: If you run Docker, old dangling images and container logs hide here. Clean them safely with:
/var/lib/docker/- (!) sudo docker system prune -a --volumes
/var/cache/apt/archives/: Cached Ubuntu package installers. Clear them with: - sudo apt-get clean
In
/home:.cache/: Hidden application caches (like pip, npm, or browser data) inside specific user folders (e.g.,/home/username/.cache). These can usually be safely deleted.- Deleted files in trash: If users delete files via a desktop environment or certain CLI tools, they often just move to
/home/username/.local/share/Trash/instead of being erased
📋 Generating the Disk Space Report
The command sequence below targets both
/home and /var. It scans down to 3 levels deep, filters out permission errors, sorts everything by size, and saves a cleanly formatted .txt report directly to your current directory. [1, 2]Run this combined block on your server:
echo "=== DISK USAGE REPORT ($(date)) ===" > disk_report.txt
echo -e "\n--- TOP DIRECTORIES IN /home ---" >> disk_report.txt
sudo du -h -d 3 /home 2>/dev/null | sort -hr | head -n 30 >> disk_report.txt
echo -e "\n--- TOP DIRECTORIES IN /var ---" >> disk_report.txt
sudo du -h -d 3 /var 2>/dev/null | sort -hr | head -n 30 >> disk_report.txt
echo -e "\n--- TOP 20 LARGEST INDIVIDUAL FILES IN BOTH ---" >> disk_report.txt
sudo find /home /var -xdev -type f -size +50M -exec du -h {} + 2>/dev/null | sort -hr | head -n 20 >> disk_report.txt
echo "Report generated successfully as 'disk_report.txt'"
Comments