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
-
To check total disk usage across all partitions, run:
df -hThis 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 -hwithdf -h /to limit output to the root directory. -
To see disk usage by directory (most exhaustive method), run:
du -sh /path/to/directoryReplace
/path/to/directorywith a specific folder (e.g.,/homeor/var/log). The-sflag sums up space for that directory, while-hformats 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 10Note: This scans only one level deep. For deeper recursion, add--max-depth=2(or remove it if you want infinite depth). -
To analyze usage by file type (e.g., logs, databases), combine
findwithdu:find /var/log -type f -printf "%s\n" | sort -n | tail -n 10This 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
findon root directories. Use-maxdepth 1for safer scanning. -
To visualize usage graphically (for large datasets), use
ncdu(install withsudo apt install ncduon Debian/Ubuntu orsudo pacman -S ncduon Arch).Run it with:
ncdu /This provides an interactive tree view, marking directories by size. Press
qto quit.
Common Mistakes
-
Forgetting to add
-h: Commands likedforduwithout-hshow numbers in bytes or kilobytes (e.g.,1048576), which is hard to interpret. Always add it. -
Running
duon mounted drives: Directories like/mntmay show incorrect sizes if files are moved or deleted from another machine. Usedf -hfor drive-level stats instead. -
Ignoring permissions: Running
findorduon restricted directories (e.g.,/etc) may requiresudo. 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.txtordu -sh /var > disk_usage_var.txt -
Use
-B 4kwithdu: For more granularity (e.g., tracking small files), reduce block size:du -B 4k -h /home -
Check metadata: Add flags to
duto see file counts or permissions:du -ah --max-depth=1 | grep total -
Automate monitoring: Create a script to run
df -hdaily:crontab -eAdd a line like0 3 * * * df -h >> /var/log/disk_usage.log.
Troubleshooting
-
No output? Verify the command runs:
ls -lhif this fails, your filesystem is corrupted. Check with:fsck /dev/sdX(replacesdXwith your partition, e.g.,sda1).If
dfis slow: Usedf -Pto skip kernel metrics and get raw filesystem stats. -
ncdunot installed: Install it via package manager (Debian/Ubuntu:apt install ncdu). If you’re on a minimal system, consider usingtree -h(sudo apt install tree) for a visual tree of folders. -
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
dudoesn’t account for compression. - Swap space or tmpfs is included in
dfbut not indu.
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.