Command lineeasyBash

Disk Space Analysis and Cleanup

Commands to analyze disk usage and find large files for cleanup.

01

The problem

Your disk is running out of space and you need to find what is consuming storage.

02

The solution

Bash
# Show disk usage summary
df -h                 # Human readable
df -i                 # Inode usage

# Check specific directory size
du -sh /path          # Summary total
du -sh *              # Each item in current dir
du -h --max-depth=1   # One level deep

# Find large files (>100MB)
find / -type f -size +100M 2>/dev/null
find . -type f -size +50M -exec ls -lh {} \;

# Sort by size
du -sh * | sort -rh
ls -lhS               # Sort files by size

# Find largest directories
du -ah /path | sort -rh | head -20

# Clean package cache (Ubuntu/Debian)
sudo apt clean
sudo apt autoclean
sudo apt autoremove

# Clean package cache (CentOS/RHEL/Fedora)
sudo yum clean all
sudo dnf clean all

# Remove old logs
sudo journalctl --vacuum-time=7d  # Keep last 7 days
sudo rm -rf /var/log/*.gz

# Find and delete core dump files
find / -name "core.*" -type f -size +10M -delete

# Remove old Docker data
docker system prune -a

# Check for deleted files still held
lsof | grep deleted

# NPM cleanup
npm cache clean --force

# Analyze with ncdu (interactive)
ncdu /path            # Install ncdu first

03

Put it to work

Example
# Find what's using space in home directory
cd ~
du -sh * | sort -rh | head -10

# Find large log files
find /var/log -type f -name "*.log" -size +100M

# Clean system
sudo apt autoremove
sudo apt clean
sudo journalctl --vacuum-time=3d

# Check disk usage
df -h
# Check inodes if deletion fails
df -i

Worth knowing

Use ncdu for interactive analysis. Large deleted files still in use appear in lsof. Check inodes if "no space" but df shows free space.