Back to Blog
Lesson 39 of the Linux: Linux Command Line for Developers course
LinuxAugust 27, 20264 min read

Monitoring Disk Usage: A Developer's Guide to df and du

Master Linux disk usage monitoring with df and du. Learn how to check filesystem capacity and identify large files to prevent server outages before they occur.

linuxcommand-linesystem-administrationdisk-usagemaintenance
A close-up of CDs and disks on a desk, featuring hands in a tech environment.

Previously in this course, we covered Advanced Redirection and Pipes to manipulate data streams. Now that you can move and filter data effectively, it’s time to ensure your server doesn't run out of storage space.

Disk exhaustion is a common "silent killer" in production environments. When a server hits 100% disk usage, logs stop writing, databases crash, and applications become unresponsive. In this lesson, we will use df and du to monitor your server's health and maintain operational stability.

Monitoring Filesystem Capacity with df

The df (disk filesystem) command is your first line of defense. It reports the amount of disk space used and available on your mounted filesystems.

When you run df without arguments, the output can be difficult to read because it defaults to 1KB blocks. Use the -h (human-readable) flag to see sizes in KB, MB, or GB.

Bash
df -h

Understanding the Output

The output columns are straightforward but vital for maintenance:

  • Filesystem: The device name (e.g., /dev/sda1).
  • Size: Total capacity of the partition.
  • Used: Amount of space currently occupied.
  • Avail: Remaining space available.
  • Use%: The percentage of space used. This is your primary indicator for when to trigger Linux Server Maintenance: Proactive Disk Health Monitoring Guide tasks.
  • Mounted on: The directory where the partition is attached.

Pinpointing Large Files with du

High stacks of organized, bound paper bundles create a structured and neat appearance.

If df shows that your disk is nearly full, you need to find what is consuming that space. This is where du (disk usage) comes in. Unlike df, which looks at the whole filesystem, du analyzes the size of specific files and directories.

To find the size of the current directory, use:

Bash
du -sh *
  • -s: Summary (only show the total for each argument).
  • -h: Human-readable format.
  • *: Matches all files and directories in the current location.

Practical Example: Cleaning the Project Logs

In our ongoing project, we have a /var/log/webapp directory. Let's inspect it to see if our logs are bloating the disk:

Bash
# Move to the logs directory
cd /var/log/webapp

# Check size of all log files
du -sh * | sort -h

The sort -h command is a helpful pipe we learned about in previous lessons; it ensures that the largest files appear at the bottom of your output, making them easy to identify.

Hands-on Exercise: Inspecting Your Server

Follow these steps to practice your disk management skills:

  1. Run df -h and identify your primary partition (usually mounted at /). What is your current Use%?
  2. Navigate to your home directory. Run du -sh * to see which subdirectories are taking up the most space.
  3. Combine du with sort to find the largest file in your /var/log folder (you may need sudo to view some system logs).
  4. Advanced: Use du -ah | sort -rh | head -n 5 to list the top 5 largest files in your current path.

Common Pitfalls

  • Ignoring the -h flag: Without human-readable output, you’ll be staring at abstract block counts that are nearly impossible to calculate mentally. Always use -h.
  • The "Deleted File" Trap: Sometimes, a file is deleted but a process still has it open. df will show the disk as full, but du won't find the file. If this happens, you must restart the service that is holding the open file handle.
  • Running du on root: Running du -sh / will attempt to calculate the size of every file on the system, which can be slow and may result in many "Permission Denied" errors. Always target specific directories.

FAQ

Q: What is the difference between df and du? A: df reports usage based on the filesystem metadata (how much space the partition has). du traverses the directory tree and calculates the size of the actual files it finds.

Q: My disk is 100% full, but du shows only 50% usage. Why? A: You likely have a large file that was deleted but is still being held by a running process. Use lsof | grep deleted to identify which process is holding that file open and restart it.

Q: Is there a more visual way to see this? A: Many Linux distributions include ncdu (NCurses Disk Usage), which provides an interactive, navigable interface for your terminal. It is highly recommended for Linux Disk Usage: How to Find and Remove Large Files tasks.

Recap

Team members presenting a project in a modern office setting with a focus on collaboration.

Maintaining your server requires active monitoring. By using df -h for a high-level view of filesystem health and du -sh for granular inspection of directories, you can identify and resolve storage issues before they impact your users.

Up next: We will explore how to manage system services, allowing you to start, stop, and enable the applications running on your server.

Similar Posts