Introduction to Disk Usage Monitoring

Welcome to this lesson on Disk Usage Monitoring. Keeping track of disk usage is essential for maintaining system health and ensuring that there is adequate storage space for applications and data. Monitoring disk usage helps system administrators detect potential issues early, avoid system crashes, and maintain optimal system performance.

Let's dive in!

Disk Usage of Filesystems with df
Disk Usage of Files and Directories with du

The df command provides statistics about certain filesystems. If you want to estimate the disk usage of files and directories we can use the du command. The -h option is used to provide a human-readable report of the disk usage for all files and directories within the specified directory. It lists each item along with its size in human-readable format.

The -sh option provides a summary of the total disk usage for the specified directory in human-readable format. It does not list individual items. Instead, it gives you the total disk usage of the entire directory at the top level.

Let's take a look at the disk usage of our $FILESYSTEM directory.

#!/bin/bash

# Display the disk usage of all files and directories under /usercode/FILESYSTEM
echo "Using -h option"
du -h $FILESYSTEM
echo ""
echo "Using -sh option"
du -sh $FILESYSTEM

The output is:

Using -h option
12K	/usercode/FILESYSTEM/.codesignal
20K	/usercode/FILESYSTEM

Using -sh option
20K	/usercode/FILESYSTEM

Let's look at the output of du -h $FILESYSTEM

  • 12K /usercode/FILESYSTEM/.codesignal: This indicates that the .codesignal directory inside /usercode/FILESYSTEM is using 12 KB of disk space.
  • 20K /usercode/FILESYSTEM: This indicates that the total disk usage of the /usercode/FILESYSTEM directory is 20 KB, which includes the space used by its contents (like the .codesignal directory).

The output of the du -sh $FILESYSTEM command is: 20K /usercode/FILESYSTEM.
This is a summarized report indicating that the total disk usage of the entire /usercode/FILESYSTEM directory is 20 KB. It does not provide a breakdown of individual files or subdirectories.

Creating and Managing Large Files
Summary and Next Steps

Excellent work! You've now learned how to:

  1. Use the df command to monitor disk space usage of filesystems.
  2. Utilize the -h option with df to present information in a human-readable format.
  3. Use the du command to estimate disk usage of files and directories.
  4. Employ the -h and -sh options with du to provide detailed and summarized reports.
  5. Create and manage large files using the fallocate command to understand their impact on disk usage.

By mastering these commands, you can quickly identify disk usage issues and take appropriate actions to resolve them. This knowledge is essential for maintaining system stability and performance. Now that you've gained a solid understanding of disk usage monitoring, it's time to put your knowledge into practice. Head over to the practice section to apply what you've learned and continue honing your skills. Happy monitoring!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal