Need to check disk usage in Linux quickly? The terminal offers powerful, one-line commands to display storage details—no GUI required. Run them directly in any Linux shell (Bash, Zsh, Fish), and you’ll see usage by partition, directory, or file type in real time.

What You'll Need

  • Access to a Linux terminal (built into most distros, including Ubuntu, Fedora, Arch, etc.)
  • A Linux system with file system access (works on servers, laptops, and desktops)
  • (Optional) A preferred output formatting preference (human-readable vs. numerical, sorted by size, etc.)

Step-by-Step

  1. To check total disk usage across all partitions, run: df -h

    This shows each mounted file system with its available space, used space, and total space—formatted for easy reading (e.g., GB, MB). The output includes the device name (like /dev/sda1) and filesystem type (ext4, XFS, etc.).

    If you want just the filesystem mounted at /: Replace df -h with df -h / to limit output to the root directory.

  2. To see disk usage by directory (most exhaustive method), run: du -sh /path/to/directory

    Replace /path/to/directory with a specific folder (e.g., /home or /var/log). The -s flag sums up space for that directory, while -h formats output in human-readable units (KB, MB, GB).

    For a top-10 list of largest directories in the current filesystem, use: du -h --max-depth=1 | sort -h | head -n 10 Note: This scans only one level deep. For deeper recursion, add --max-depth=2 (or remove it if you want infinite depth).

  3. To analyze usage by file type (e.g., logs, databases), combine find with du: find /var/log -type f -printf "%s\n" | sort -n | tail -n 10

    This lists the 10 largest files in the logs directory. For a more organized breakdown, try: find /var/log -type f -exec du -h {} +

    Important: Be cautious with find on root directories. Use -maxdepth 1 for safer scanning.

  4. To visualize usage graphically (for large datasets), use ncdu (install with sudo apt install ncdu on Debian/Ubuntu or sudo pacman -S ncdu on Arch).

    Run it with: ncdu /

    This provides an interactive tree view, marking directories by size. Press q to quit.

Common Mistakes

  • Forgetting to add -h: Commands like df or du without -h show numbers in bytes or kilobytes (e.g., 1048576), which is hard to interpret. Always add it.
  • Running du on mounted drives: Directories like /mnt may show incorrect sizes if files are moved or deleted from another machine. Use df -h for drive-level stats instead.
  • Ignoring permissions: Running find or du on restricted directories (e.g., /etc) may require sudo. Omit it only if you’re scanning your home folder.

Tips

  • Save output to a file: Redirect results for later review: df -h > disk_usage.txt or du -sh /var > disk_usage_var.txt
  • Use -B 4k with du: For more granularity (e.g., tracking small files), reduce block size: du -B 4k -h /home
  • Check metadata: Add flags to du to see file counts or permissions: du -ah --max-depth=1 | grep total
  • Automate monitoring: Create a script to run df -h daily: crontab -e Add a line like 0 3 * * * df -h >> /var/log/disk_usage.log.

Troubleshooting

  1. No output? Verify the command runs: ls -lh if this fails, your filesystem is corrupted. Check with: fsck /dev/sdX (replace sdX with your partition, e.g., sda1).

    If df is slow: Use df -P to skip kernel metrics and get raw filesystem stats.

  2. ncdu not installed: Install it via package manager (Debian/Ubuntu: apt install ncdu). If you’re on a minimal system, consider using tree -h (sudo apt install tree) for a visual tree of folders.
  3. Commands return "No space left on device": Check for full partitions with: df --output pcent,target | grep -E "^[0-9]*[.]?[0-9]+" Free up space by deleting unnecessary files or expanding storage.

Frequently Asked Questions

How do I check disk usage in real-time?

Use du --max-depth=1 with watch to refresh results every second: watch -n 1 du -sh /

This updates the largest directory sizes dynamically.

What’s the fastest way to spot a 1GB file?

Run: sudo find / -type f -size +1G -exec ls -lh {} +

This filters for files larger than 1GB and lists their details.

Can I see disk usage for a specific user?

Use: du -sh /home/username

Replace username with the actual user. For all users’ home folders: for user in $(cut -d: -f1 /etc/passwd); do du -sh /home/$user; done

Warning: This may take time and use significant resources.

Why does df show different numbers than du?

df reports filesystem-level statistics (e.g., blocks used), while du measures file sizes. Differences occur when:

  • Files are fragmented across partitions.
  • Files are compressed (e.g., zipped archives) but du doesn’t account for compression.
  • Swap space or tmpfs is included in df but not in du.

For a precise comparison, use du -sh --apparent-size to show uncompressed sizes.

How do I clear temporary files to free up space?

For Linux systems, run: sudo journalctl --vacuum-size=100M --vacuum-time=24h

This limits log storage. For other temp files, check: ls /tmp | xargs rm -f

Or use system tools like clean-my-cache (install with apt install clean-my-cache).

What if I get a "Permission denied" error?

Use sudo before commands (e.g., sudo du -sh /var), or modify permissions for your user: sudo chmod -R u+rwx /path/to/directory

Only do this for directories you own or have explicit write access to.