Linux commands to check disk usage are your go-to tool for identifying where storage space is being consumed—whether you're troubleshooting slow file systems, freeing up space for new projects, or preparing for upgrades. Unlike graphical tools like `df` with a GUI, these commands run directly in the terminal and provide precise, formatted output for every filesystem, partition, and directory. You’ll need administrative privileges for full results, but basic checks work without them. Common commands include `du` (disk usage), `lsblk`, and `df`—each serving different purposes, from per-directory scrutiny to system-wide summaries.
What You'll Need
- A Linux system (Ubuntu, Fedora, Arch, Debian, or others).
- A terminal emulator (or console if using a minimal setup).
- Basic Linux knowledge of commands (optional but helpful).
- Administrative/user permissions for full system checks (may require `sudo`).
Step-by-Step
- Check overall disk space usage with `df -h`:
Run this command in the terminal to see a human-readable breakdown of each mounted filesystem’s available and used space. The `-h` flag formats output in human-readable units (KB, MB, GB). Example:
df -hOutput will list partitions like `/dev/sda1`, `/dev/nvme0n1p2`, or `/boot`, showing total, used, available, and used percentages alongside mount points (e.g., `/`, `/home`).
If you want to filter output by a specific filesystem (e.g., only `/`), append the mount point:
df -h / - Inspect file/folder space with `du`:
Use `du` to measure space consumed by individual files or directories. The `-h` flag adds readability, and `--max-depth=N` limits depth (e.g., `--max-depth=1` for immediate subdirectories). Example:
du -h --max-depth=1 /var/logThis shows the size of `/var/log` and its immediate subfolders. To find the largest files in a directory:
du -h /path/to/dir | sort -hr | head -n 10(Sorts by size descending and lists top 10 entries.)For hidden files (those starting with `.`), add `-a`:
du -ah /path/to/dir - List block devices with `lsblk`:
Useful for identifying partitions (e.g., `/dev/sdb1`) and their sizes in a tabular format. Example:
lsblkOutput includes device names, sizes, and mount points. Append `-o NAME,SIZE,FSTYPE` to customize columns:
lsblk -o NAME,SIZE,FSTYPE - Check disk usage by process with `top` or `htop`:
To see which running processes are consuming disk I/O, run:
top -o %disk(Sorts by disk usage percentage.) For a more user-friendly view, install `htop` (Linux packages usually include it):sudo apt install htop(Debian/Ubuntu) or your distro’s equivalent, then run `htop`. - Analyze disk usage graphically with `ncdu`:
Install `ncdu` (NCurses Disk Usage) for an interactive tool that visually breaks down space by directory. Example:
sudo apt install ncdu(Debian/Ubuntu) or your distro’s package manager, then run:ncdu /Navigate with arrow keys, press `d` to delete files, and `q` to exit. This is the most efficient way to delete unnecessary files without manual copying.
Common Mistakes
- Running `du` on the root directory (`/`): This can cause delays or errors as it scans all files recursively. Always specify a subdirectory (e.g., `/home`) or use `--max-depth` to limit scope.
- Ignoring `-h` with `df` or `du`: Without `-h`, output may appear overwhelming with tiny units like bytes. Always use `-h` for readability.
- Not using `sudo` for system-wide checks: Commands like `df` or `du` on root may require elevated privileges. Use `sudo` or log in as root to avoid permission errors.
- Overlooking hidden files in `du`: Hidden files (starting with `.`) can bloat disk usage. Use `-a` to include them or filter out with `grep`:
du -sh * | grep "\." - Using incorrect device names in `lsblk`: Labels can differ by partition or filesystem. Always verify with `lsblk` before assuming a device name (e.g., `/dev/sda1` vs `/dev/nvme0n1p2`).
Tips
- Combine commands for efficiency:
To find the largest directories quickly, pipe `du` output to `sort` and `head`:
du -sh / | sort -hr | head -n 5(Lists top 5 directories by size.) - Use `df` to identify misbehaving mounts:
Look for filesystems with low available space (e.g., `< 1G`) to prioritize cleanup:
df -h | grep -E "E[0-9]{2,3}%" | awk '{print $1, $4}' - Clean up frequently used files:
Common culprits include:
- Old backups (`tar.gz`, `.zip` files).
- Cache directories (e.g., `~/.cache`, `/var/cache`).
- Temporary files (`/tmp`, `/var/tmp`).
- Duplicate files (use `du` + `find` to locate them).
- Automate checks with cron:
Schedule regular disk usage reports using `cron`. Example:
crontab -eAdd:0 3 * * * df -h > /var/log/disk_report_`date +\%Y-\%m-\%d`.txt(Saves a daily report to `/var/log`). - Use `ncdu` for interactive cleanup:
`ncdu` lets you delete files directly from the terminal without navigating manually. Example:
ncdu /home/user/Downloads(Highlights large files; press `d` to delete.)
Troubleshooting
- Command not found errors:
If `du`, `ncdu`, or `lsblk` isn’t recognized, install it:
- Debian/Ubuntu:
sudo apt install du, ncdu, lsblk - Fedora/RHEL:
sudo dnf install du, ncdu, lsblk - Arch Linux:
sudo pacman -S du ncdu lsblk
- Debian/Ubuntu:
- Permission denied when running `df` or `du`: Use `sudo` or switch to a user account with administrative privileges. If you’re already using `sudo`, ensure the command (e.g., `df -h`) isn’t misquoted or lacks proper flags.
- Slow performance with `du` scans:
Large directories may hang or run slowly. Limit depth with `--max-depth=N` or scan smaller subdirectories first. Example:
du -sh --max-depth=1 /var/log | grep "10G"(Finds directories near 10GB size.) - Output looks empty or incorrect:
Verify the mount point is correct. If scanning `/`, ensure the filesystem is mounted (run
mount | grep /to confirm). For cloud/remote storage (e.g., NFS), check if the filesystem is properly exported. - `df` shows no partitions:
This can happen if no filesystems are mounted. Check with:
mount | grep -v NODEV(Lists all mounted filesystems). If nothing appears, no filesystems are attached to the system. - Need help with a specific distro: Distros vary slightly in package names for tools like `ncdu` (e.g., `ncdu` on Arch vs `ncdu` on Debian). Check your distro’s package manager or search for the tool’s name + your OS version.
Frequently Asked Questions
How do I check disk space for a specific user’s home directory?
Use `du` with the user’s home path:du -sh ~/Downloads
To compare against the system-wide log directory:
du -sh /var/log | wc -l
(Counts lines in `/var/log` output to estimate size.)
Why is my disk usage suddenly high?
Common causes include:- Large downloads (e.g., media files, backups).
- Unused temporary files (e.g., browser caches, build artifacts).
- Log files from services (e.g., `journalctl` logs, `nginx` logs).
- Duplicated files (e.g., old backups left in place).
Can I use `df` to see which app is using disk space?
No—`df` only shows filesystem-level usage. To identify culprits, use `top -o %disk` or `htop` to see processes writing to disk. For specific apps, check logs (e.g., `journalctl -u nginx --since "yesterday"`).How do I free up space without deleting important files?
Instead of permanent deletion:- Move files to a cloud storage service (e.g., Dropbox, Nextcloud).
- Use `ncdu` to delete only large, unnecessary files. Example:
ncdu /home/user/DownloadsPress `d` to confirm deletion of files >100MB. - Compress archives (e.g., `.tar.gz`) to free up space temporarily.
What if my disk is full but `df` shows ample space?
This can happen due to:- Filesystem quota limits: Some systems cap user disk usage. Check with:
quota -v(Requires `quota` package and permissions.) - Filesystem corruption: Run `fsck` to check for errors:
sudo fsck -f /dev/sdX(Replace `sdX` with your partition, e.g., `sda1`). - Hidden files: Use `du -ah` to find hidden files (e.g., `.cache`, `.config`).
How do I monitor disk usage in real-time?
Install `inotify-tools` (Linux package) and run:inotifywait -r -e modify /path/to/dir
(Shows when files change in real-time.) For a GUI alternative, use `gnome-disks` (Ubuntu) or `Disks` (GNOME), which show live usage graphs.
Need more details on a specific distro or command? Let us know which one you’re using!