The fastest way to check disk space in Linux is with the built-in `df` (disk filesystem) command, which shows mounted drives and their remaining capacity. For a quick breakdown by directory, use `du` (disk usage) with `-h` for human-readable sizes. No extra tools or permissions are needed—just run these commands in the terminal.

What You'll Need

  • A Linux system (Ubuntu, Fedora, Arch, etc.) with terminal access.
  • Root (admin) permissions if you need to inspect system directories.
  • A basic understanding of Linux commands.

Step-by-Step

  1. Check available disk space on the root filesystem: Open your terminal and run: df -h

    This lists all mounted filesystems with: - Total space - Used space - Available space - Percentage used Example output: Filesystem Size Used Avail Use% Mounted on /dev/sda1 50G 25G 20G 50% /

    If you want details per partition (like `/boot`, `/home`), add -T to identify filesystem type (e.g., df -Th for ext4).

  2. Analyze space usage in a specific directory: Navigate to the folder (e.g., cd ~/Downloads) and run: du -sh * | sort -rh

    -s shows total size, -h makes it human-readable (e.g., KB, MB, GB), and sort -rh sorts largest first. Example: 5.2G /home/user/Downloads/large_file.zip 2.4G /home/user/Downloads/unused_backups/

    For recursive analysis of subdirectories, add -a or omit * to check all files:

    du -sh /path/to/folder/ | sort -rh

    Example for the root filesystem: du -sh / | sort -rh.

  3. Find which processes are consuming disk I/O: Use iotop (install with sudo apt install iotop on Debian/Ubuntu or sudo pacman -S iotop on Arch). Run: sudo iotop

    This shows real-time disk activity by user/process. Exit with Ctrl+C.

  4. Check log files for large disk usage: Logs often fill up unexpectedly. Run: sudo du -sh /var/log/* | sort -rh

    Example output: 1.2G /var/log/syslog 340M /var/log/auth.log

    If logs are bloated, rotate them with logrotate (e.g., sudo logrotate -f /etc/logrotate.conf).

Common Mistakes

  • Ignoring output formatting: Without -h in du, sizes appear in bytes (e.g., 123456), making them hard to interpret. Always use -h.
  • Running as root without cause: Use sudo only when necessary (e.g., for iotop or df on system partitions). Root commands can reveal sensitive info.
  • Overlooking recursive checks: For large directories (e.g., /home), omit * in du to scan all files.

Tips

  • Use `ncdu` for a user-friendly disk analyzer: Install via sudo apt install ncdu (Debian/Ubuntu) or brew install ncdu (macOS). Run ncdu / to navigate directories interactively with arrow keys and delete files with d.
  • Monitor space over time: Use watch -n 5 df -h to update disk stats every 5 seconds. Replace -n 5 with seconds.
  • Check cloud storage usage: For cloud volumes (e.g., /mnt/ccloud/vol1), use: du -sh /mnt/ccloud/* | sort -rh

    Replace /mnt/ccloud with your cloud mount point.

  • Sort by largest files first: Add | sort -n to du commands to list smallest files first (reverse with sort -nr).

Troubleshooting

  1. Command not found: Install missing tools:
    • Debian/Ubuntu: sudo apt install du command (e.g., du, df)
    • Arch Linux: sudo pacman -S du df iotop
    • Fedora: sudo dnf install du command (e.g., du, df)

    For iotop, some distros require sudo apt install iptables first.

  2. Output shows zero space: Check if /proc/fs/bdev is corrupted. Reboot or run: sudo fsck /dev/sdX (replace sdX with your disk, e.g., sd1).
  3. High CPU usage during analysis: Run du in the background with & and check output later: du -sh /path/to/folder &

    Or use time du -sh /path/to/folder to measure time taken.

  4. Permission denied: Change directory to a writable location (e.g., cd /tmp) or use sudo du only for system checks.

    For df, ensure you’re inspecting the correct partition (e.g., /dev/sda1, not a cloud volume).

  5. Commands return no output: Verify the filesystem is mounted. Run: mount | grep

    Example: mount | grep sda1. If nothing appears, check with lsblk to confirm the disk exists.

  6. Filesystem is full but no files exist: Check for system caches or temporary files: du -sh /tmp /var/tmp /run /sys/fs/cgroup

    If /run is full, restart services (e.g., sudo systemctl restart apache2).

Frequently Asked Questions

1. How do I check disk space on a remote Linux server via SSH?

Use the same commands over SSH. Example: ssh user@server "df -h" or ssh user@server "du -sh /var/log/*".

2. What if I see a filesystem with 100% usage but no files to delete?

The filesystem might be a swap partition, logs, or a system cache. Check with: df -hT to identify the type (e.g., swap). For logs, rotate them with sudo logrotate -f /etc/logrotate.conf.

3. Can I use these commands on macOS instead of Linux?

No. macOS uses its own tools like diskutil (e.g., diskutil space) or GUI tools (Finder > About This Mac). For Linux, these commands are native.

4. How do I exclude specific directories from disk analysis?

For du, use find: find /path/to/folder -type d ! -path "*/exclude/*" -exec du -sh {} + Example to exclude /tmp: find /home -type d ! -path "*/tmp/*" -exec du -sh {} +.

5. What if the disk is full but I can’t find what’s using space?

Enable kernel tracing with sudo dmesg | tail or check for large inodes (small files) with: sudo journalctl -p 5 -b | grep "inode" or use ncdu to explore interactively.

6. Should I use ls -lh instead of du?

ls -lh lists files but not total sizes. For accurate usage, always use du. ls is useful for listing files, not analyzing space.