To check disk space in Unix smoothly, use the command `df -h`. It shows all mounted filesystems with their available, used, and total space—human-readable with kilobytes, megabytes, or gigabytes. No extra tools needed. If you need deeper insights, `du` or `ncdu` reveal directory-level usage.

What You'll Need

  • The Unix/Linux terminal (accessible via SSH, Linux distro, or macOS Terminal).
  • Basic command-line familiarity (though no prior experience is required).
  • Root/sudo access if troubleshooting partition-specific issues.

Step-by-Step

  1. Basic disk usage summary Run `df -h` to see all mounted filesystems with available space. The output includes:
    • /dev/sda1 (your root partition) 10G 8.2G 1.8G 82% /
    • /dev/nvme0n1p2 200G 150G 50G 75% /mnt/data
    The last column shows the percentage used. If any filesystem shows "100%" or "no space left," you have a critical issue.
  2. Targeted directory usage To check space inside a specific folder (e.g., `/home/user`), use: du -sh /home/user
    • -s = summary (total size only)
    • -h = human-readable format
    For a more detailed breakdown, install ncdu (Netcat Disk Usage) via package manager (e.g., sudo apt install ncdu on Debian/Ubuntu). Navigate with arrow keys, then press d to delete files.
  3. Find largest consuming directories Use `du` with filters to identify bloated directories: sudo du -ah /path/to/directory | sort -hr
    • Sorts results by size in descending order.
    • Replace `/path/to/directory` with the root (e.g., `/` for system-wide analysis).
    Example output: 12T /var/log 2.5G /tmp 1.8G /home/user/Downloads Focus on the largest values first.
  4. Check for corrupted ext4 partitions If disk usage misreports space, the filesystem might be corrupted. Run: sudo fsck -fy /dev/sdX
    • Replace sdX with your actual partition (e.g., sda1).
    • -f = force check (no mounts needed)
    • -y = automatic repair
    • Only do this on inactive systems or after backing up critical data.
    If that did not work: Boot from a live USB, mount the partition read-only, and inspect `/proc/mounts` or `dmesg` for errors.
  5. Free up space manually Common actions:
    1. Delete old logs: sudo journalctl --vacuum-size=100M (limits log size).
    2. Clear cache: sudo apt clean (Debian/Ubuntu) or rm -rf /tmp/*. Avoid rm -rf /—it wipes the OS!
    3. Remove unused packages: sudo apt autoremove.
    4. Delete large files: Use `ncdu` to navigate and delete files directly.

Common Mistakes

  • Misinterpreting `df` output: Filesystems with "Use%" at 100% appear full, but they might still have space (e.g., swap partitions). Always check the "Avail." column.
  • Deleting system directories: Never delete `/var`, `/home`, or `/usr` manually. Use packages or tools like `apt purge` instead.
  • Skipping root checks: Root partitions (e.g., `/`) often have the highest usage. Use `du -sh /` to verify.
  • Ignoring hidden files: Commands like `du` ignore hidden files by default. Add `./` to include them: du -ah /path.

Tips

  • Schedule regular checks: Add a cron job to monitor disk space weekly. Example: 0 3 * * 0 /usr/bin/df -h >> /var/log/disk_usage.log This logs disk space at 3 AM Sundays to `/var/log/disk_usage.log`.
  • Use `df --total` for system-wide totals: Shows the total available space across all filesystems.
  • Prefer `ncdu` for interactive analysis: It highlights largest files and lets you delete them without leaving the terminal.
  • Check for swap space: If physical space is low, swap can inflate usage. Monitor with free -h.

Troubleshooting

  • Disk appears full but commands show space:
    1. Check for zombie processes: ps aux | grep -i 'sleep\|awk'.
    2. Look for leftover temporary files in `/tmp` or `/var/tmp`.
    3. Reinstall unnecessary services (e.g., old software daemons).
  • Commands hang or slow down:
    1. Run `htop` (install with sudo apt install htop) to identify resource-hungry processes.
    2. Kill resource-intensive apps: kill -9 .
  • System refuses to boot after checking:
    1. Revert filesystem changes using a live USB.
    2. Check for corrupted filesystems with fsck (as shown in Step 4).
  • Permissions denied on directories:
    1. Use `sudo` or add your user to the `sudoers` group.
    2. For root checks, switch to root: sudo su.

Frequently Asked Questions

How do I check disk space for a specific user's home directory?

Use du -sh ~/ to get a summary. For finer control, install ncdu and navigate to ~/ with arrow keys.

Why is my disk space showing 0% even after deleting files?

This often means the filesystem is mounted as read-only or a metadata issue exists. Check with mount or dmesg for errors.

Can I permanently free up disk space without reinstalling?

Yes! Use tools like autoremove, journalctl --vacuum-time=7d, or ncdu to delete old logs, unnecessary packages, and large files. Avoid rm -rf /!

How does swap space affect my disk usage?

Swap is not disk space—it's virtual memory. Use free -h to see swap usage. Freeing swap doesn't reduce your actual disk space but can improve performance.

What if my system won't run commands because of low space?

Log in via SSH from another machine, run commands remotely, or boot to a live USB to check space interactively.

Is there a way to monitor disk space in real-time?

Use tools like watch -n 5 df -h (refreshes every 5 seconds) or install a GUI monitor like gnome-disks (GUI for disk monitoring).