What I Wanted to Do

I wanted to check disk usage on a Linux server. Using df and du, you can check the disk free space and folder sizes.

df: Check Overall Disk Usage

df -h                    # Display in human-readable format
df -h /                  # Show root only
df -h /var               # Specify a particular path

Example output

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        50G   20G   28G  42% /

du: Check Folder Size

du -sh foldername        # Total size of a folder
du -sh /*                # Each folder directly under root
du -sh /var/log/*        # Contents of the log folder
du -h --max-depth=1 /var # Show only one level deep

Handling a Full Disk

Find large files

find / -size +100M -type f 2>/dev/null

Delete log files

sudo journalctl --vacuum-size=100M    # Trim systemd logs to 100MB or less
sudo find /var/log -name "*.log" -mtime +30 -delete  # Delete logs older than 30 days

Remove unused Docker data

docker system prune -a

Common Pitfalls

  • df checks the whole filesystem; du checks a specific folder’s size
  • The -h option displays sizes in human-readable format (GB/MB)
  • When using Docker, /var/lib/docker tends to grow large

If your VPS is running low on RAM, you can ease memory pressure by adding swap space using How to Set Up Swap on Linux.

Looking for reliable cloud infrastructure? Check out these developer-friendly services.