πŸ’Ύ Linux/Unix: Storage, Archiving & Compression
Estimated reading: 3 minutes 22 views

πŸ’½ Linux/Unix: Disk Usage Commands – df, du Explained with Examples

🧲 Introduction – Why Learn Disk Usage Commands in Linux?

Running out of disk space can lead to failed installs, corrupted logs, and halted services. Knowing how to monitor disk usage is essential for maintaining a healthy Linux/Unix system. Commands like df and du help you quickly identify disk space usage by filesystem or directory, so you can clean up and plan effectively.

🎯 In this guide, you’ll learn:

  • How to check free and used disk space with df
  • How to find large files and folders with du
  • Real-world examples and practical flags for scripting or analysis

πŸ“Š 1. df – Display Filesystem Disk Space Usage

βœ… What is df?

The df (disk free) command shows used and available space on mounted filesystems.

πŸ› οΈ Syntax:

df [options] [path]

πŸ”Ή Common Options:

OptionDescription
-hHuman-readable (e.g., MB, GB)
-TShow filesystem type
-xExclude filesystem type
--totalShow grand total summary

πŸ§ͺ Example 1: Show space on all mounted filesystems

df -h

πŸ“€ Output:

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        50G   32G   15G  69% /
tmpfs           1.9G     0  1.9G   0% /dev/shm

🧠 Useful for checking overall system usage.


πŸ§ͺ Example 2: Check specific mount point

df -h /var

πŸ§ͺ Example 3: Include filesystem type

df -hT

πŸ“€ Output:

Filesystem     Type  Size  Used Avail Use% Mounted on
/dev/sda1      ext4   50G   32G   15G  69% /

πŸ“‚ 2. du – Estimate File & Directory Space Usage

βœ… What is du?

The du (disk usage) command shows the space used by directories or files, useful for finding large folders.

πŸ› οΈ Syntax:

du [options] [path]

πŸ”Ή Common Options:

OptionDescription
-hHuman-readable sizes
-sSummarize total only
-aShow all files and subdirs
--max-depth=NLimit recursion depth
--exclude=PATTERNIgnore certain paths

πŸ§ͺ Example 1: Check size of a folder

du -sh /home/user

πŸ“€ Output:

2.1G	/home/user

πŸ§ͺ Example 2: Show size of all subfolders in /var

du -h --max-depth=1 /var

πŸ“€ Output:

200M	/var/log
3.5G	/var/lib
120K	/var/spool

🧠 Quickly locate heavy directories.


πŸ§ͺ Example 3: Find top 10 largest folders

du -ah / | sort -rh | head -n 10

πŸ“€ Output:

4.1G	/var/lib/mysql
2.0G	/home/user/Videos
1.5G	/opt/docker/images

🧠 Tool Comparison: df vs du

Featuredfdu
Shows usage ofFilesystems (mount points)Individual directories/files
Ideal forChecking total disk spaceFinding space hogs
Default unitKB (use -h for human-readable)KB (use -h)
RecursionβŒβœ…

πŸ“Œ Summary – Recap & Next Steps

The df and du commands are essential tools for disk space auditing, cleanup planning, and managing storage resources on Linux systems. Use them regularly to monitor space usage, prevent full disks, and optimize performance.

πŸ” Key Takeaways:

  • Use df -h to see how much space is left on mounted filesystems.
  • Use du -sh to check how large a folder is.
  • Combine du with sort to find large directories consuming space.

❓ FAQs

❓ Why does du and df show different values?
βœ… df checks free space on the filesystem level; du scans actual file sizes. Differences may include reserved space or open-but-deleted files.

❓ How to find large files using du?
βœ… Try:

du -ah / | sort -rh | head -n 10

❓ How do I check disk usage on a mounted USB drive?
βœ… Use:

df -h /mnt/usb

❓ Can I exclude certain folders from du?
βœ… Yes:

du -h --exclude=/home/user/.cache /home/user

❓ Is there a GUI alternative to du?
βœ… Yes, tools like baobab or ncdu (CLI visual tool) offer easier views.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

πŸ”΅ Linux/Unix: Disk Usage (df, du)

Or Copy Link

CONTENTS
Scroll to Top