Disk Usage Analysis

LinuxBeginner
Practice Now

Introduction

To clean up a full hard drive, you need to answer:

  1. "Where are the huge boxes?" (Heavy directories).
  2. "Where are the giant items?" (Large files).

Tools:

  • find: Metal Detector (Finds files by size).
  • du: Scale (Weighs directories).
  • sort: Organizer (Ranks by size).

In this lab, you will perform a professional "Spring Cleaning" audit.

This style of analysis is common in production work. When a filesystem fills up, the real question is usually not "Is it full?" but "Which exact files or directories are responsible?"

Find Large Files (find)

Use find to catch "Big Fish" (files > 100MB).

cd ~/project
find . -type f -size +100M

Breakdown:

  • .: Current directory.
  • -type f: Files only.
  • -size +100M: Larger than 100 Megabytes.

The find command is powerful because it searches by rules instead of by manually browsing directories. That makes it especially useful on large or messy filesystems.

Output:

./storage/backup_v1.iso

Save the report:

find . -type f -size +100M > large_files.txt
cat large_files.txt

Analyze Directory Sizes (du)

Sometimes a "Junk Drawer" (many small files) is the problem.

Weigh /var (Variable data) using du:

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

Breakdown:

  • -h: Human readable (MB, GB).
  • --max-depth=1: Only show top-level subfolders (Crucial!).

Output:

246M    /var/cache
5.5G    /var/lib
...

Now you know /var/lib is the heavy box.

Directory-level analysis is useful because storage problems are often caused either by one giant file or by one area of the filesystem accumulating lots of data over time.

Rank Largest Directories (sort)

Scrambled lists are hard to read. Use sort to organize them.

Combine du, sort, and head for the "Golden Combo".

sudo du -h --max-depth=1 /var | sort -hr | head -n 5 > ~/project/top_var_dirs.txt

Breakdown:

  • du ...: Get sizes.
  • sort -hr: Sort Human sizes in Reverse (Biggest first).
  • head -n 5: Show only top 5.

Check report:

cat ~/project/top_var_dirs.txt

This is a standard sysadmin workflow.

That workflow is worth remembering: locate the large area, sort the results, and then focus cleanup efforts where they will have the biggest effect first.

Summary

In this lab, you learned to play "Detective" with your disk space.

  • find -size: helped you catch the specific large files (The "Big Fish").
  • du -h --max-depth=1: helped you weigh the main folders (The "Heavy Boxes").
  • sort -hr: helped you rank them to prioritize your cleanup.

Using these tools, you can quickly answer the question: "Why is my server full?" and fix it before it becomes a problem.