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
- Check available disk space on the root filesystem:
Open your terminal and run:
df -hThis 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
-Tto identify filesystem type (e.g.,df -Thfor ext4). - Analyze space usage in a specific directory:
Navigate to the folder (e.g.,
cd ~/Downloads) and run:du -sh * | sort -rh-sshows total size,-hmakes it human-readable (e.g., KB, MB, GB), andsort -rhsorts largest first. Example:5.2G /home/user/Downloads/large_file.zip 2.4G /home/user/Downloads/unused_backups/For recursive analysis of subdirectories, add
-aor omit*to check all files:du -sh /path/to/folder/ | sort -rhExample for the root filesystem:
du -sh / | sort -rh. - Find which processes are consuming disk I/O:
Use
iotop(install withsudo apt install iotopon Debian/Ubuntu orsudo pacman -S iotopon Arch). Run:sudo iotopThis shows real-time disk activity by user/process. Exit with
Ctrl+C. - Check log files for large disk usage:
Logs often fill up unexpectedly. Run:
sudo du -sh /var/log/* | sort -rhExample output:
1.2G /var/log/syslog 340M /var/log/auth.logIf logs are bloated, rotate them with
logrotate(e.g.,sudo logrotate -f /etc/logrotate.conf).
Common Mistakes
- Ignoring output formatting: Without
-hindu, sizes appear in bytes (e.g., 123456), making them hard to interpret. Always use-h. - Running as root without cause: Use
sudoonly when necessary (e.g., foriotopordfon system partitions). Root commands can reveal sensitive info. - Overlooking recursive checks: For large directories (e.g.,
/home), omit*induto scan all files.
Tips
- Use `ncdu` for a user-friendly disk analyzer:
Install via
sudo apt install ncdu(Debian/Ubuntu) orbrew install ncdu(macOS). Runncdu /to navigate directories interactively with arrow keys and delete files withd. - Monitor space over time: Use
watch -n 5 df -hto update disk stats every 5 seconds. Replace-n 5with seconds. - Check cloud storage usage: For cloud volumes (e.g.,
/mnt/ccloud/vol1), use:du -sh /mnt/ccloud/* | sort -rhReplace
/mnt/ccloudwith your cloud mount point. - Sort by largest files first: Add
| sort -ntoducommands to list smallest files first (reverse withsort -nr).
Troubleshooting
- 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 requiresudo apt install iptablesfirst. - Debian/Ubuntu:
- Output shows zero space:
Check if
/proc/fs/bdevis corrupted. Reboot or run:sudo fsck /dev/sdX(replacesdXwith your disk, e.g.,sd1). - High CPU usage during analysis:
Run
duin the background with&and check output later:du -sh /path/to/folder &Or use
time du -sh /path/to/folderto measure time taken. - Permission denied:
Change directory to a writable location (e.g.,
cd /tmp) or usesudo duonly for system checks.For
df, ensure you’re inspecting the correct partition (e.g.,/dev/sda1, not a cloud volume). - Commands return no output:
Verify the filesystem is mounted. Run:
mount | grepExample:
mount | grep sda1. If nothing appears, check withlsblkto confirm the disk exists. - Filesystem is full but no files exist:
Check for system caches or temporary files:
du -sh /tmp /var/tmp /run /sys/fs/cgroupIf
/runis 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.