How to Check and Manage Disk Space in Ubuntu: Complete Guide for Beginners and Advanced Users

目次

1. Introduction

Ubuntu is widely used not only for personal use but also for server operations due to its lightweight and stable environment. However, as you continue to use the system, disk space will inevitably start running low. Insufficient disk space can lead to reduced system performance and even failures when installing new software.

This article explains how to check and properly manage disk space in Ubuntu. It covers the usage of CLI (Command Line Interface) tools such as the df and du commands, as well as the GUI tool “Disk Usage Analyzer,” which allows you to visually inspect storage usage. Clear examples and step-by-step instructions will help beginners follow along with confidence.

2. How to Check Overall Disk Usage (df Command)

To check the overall disk usage in Ubuntu, use the df command. This convenient tool displays disk usage and available space for each file system. In this section, we will explain everything from basic usage to advanced examples.

What Is the df Command?

df stands for “disk free” and is used in Linux and Unix-based operating systems to check disk usage and available capacity. It is simple, fast, and ideal for immediately understanding the system’s disk status.

Basic Usage

The following is the most common way to use the df command:

df -h
  • -h option
    Displays values in a human-readable format with units—for example, instead of showing “1024000,” it displays “1G” or “500M.”

Example Output

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        50G   20G   30G   40% /
tmpfs           500M     0  500M    0% /dev/shm

Explanation of Output

  • Filesystem: Type of file system used (e.g., ext4, tmpfs).
  • Size: Total size of the file system.
  • Used: Amount of space currently in use.
  • Avail: Available free space.
  • Use%: Percentage of disk usage.
  • Mounted on: Location where the file system is mounted.

Advanced Usage Examples

Display Only Specific File Systems

You can include file system type information using the -T option and even filter specific file systems.

df -T ext4

This displays only ext4 file system information.

Specify a File System to Check

If you want to check a specific mount point (e.g., /home), execute:

df -h /home

This displays the disk usage and available space allocated to the /home directory.

Troubleshooting Tips

  • If the disk is full
    Use df to identify file systems that are at 100% usage and delete unnecessary files.
  • If df results do not update
    Deleted files may still be used by processes. Identify them using the lsof command:
lsof | grep deleted

Summary

The df command is a simple yet powerful tool to check system-wide disk usage in Ubuntu. With the -h option, results are easier to understand, making it ideal for beginners. Mastering the basics and advanced usage discussed here will help you manage disk storage efficiently.

3. How to Check Usage of Specific Directories and Files (du Command)

Knowing the overall disk usage is not enough to determine which directories or files are consuming space. In such cases, use the du command to investigate disk usage per directory or file. This section explains both basic and advanced usage of the du command.

What Is the du Command?

du stands for “disk usage” and displays the disk space used by specified directories or files. It is highly useful for identifying the cause of disk space shortages.

Basic Usage

Example of a simple du command:

du -sh /path/to/directory
  • -s option
    Displays only the total usage of the directory.
  • -h option
    Shows output in human-readable units (KB, MB, GB).

Example Output

5.2G    /home/user/Documents

This indicates that the /home/user/Documents directory is using 5.2GB of space.

Check Detailed Usage

Display Subdirectories

du -h /path/to/directory/*

Example

1.5G    /path/to/directory/subdir1
3.2G    /path/to/directory/subdir2
500M    /path/to/directory/subdir3

Advanced Usage Examples

Sort by Size

du -ah /path/to/directory | sort -rh | head -n 10
  • -a: Displays both files and directories.
  • sort -rh: Sorts in descending order.
  • head -n 10: Shows the top 10 results.

Example

2.5G    /path/to/directory/largefile1.iso
1.2G    /path/to/directory/subdir1
800M    /path/to/directory/largefile2.zip

Search for Files Matching Conditions

find /path/to/directory -name "*.log" -exec du -h {} +

Troubleshooting Tips

  • If du results do not match expected values, deleted files may still be held by processes. Use:
lsof | grep deleted

Summary

The du command helps identify which directories and files are consuming disk space. Combining it with commands like find or sort allows for efficient investigation and cleanup.

4. How to Check Disk Space with a GUI (Disk Usage Analyzer)

Besides CLI tools, Ubuntu also provides a GUI method to check disk usage using “Disk Usage Analyzer.” This section explains its features, installation, and usage.

What Is Disk Usage Analyzer?

Disk Usage Analyzer is a default Ubuntu tool that visually displays disk usage with graphs, making it easy to identify storage-heavy directories and files.

Installation

sudo apt update
sudo apt install baobab

Basic Usage

  1. Launch from the applications menu.
  2. Or run from a terminal:
   baobab

Scan Options

  • Scan home folder
  • Select specific directory
  • Scan remote disks

View Usage

  • Graph view
  • Detailed list

Advantages and Disadvantages

Advantages

  • Easy to understand visual format
  • No command knowledge required
  • Supports network drives

Disadvantages

  • Can be slow with large directories
  • Less customizable than CLI tools

Summary

Disk Usage Analyzer is useful for users who prefer visual tools. When combined with CLI methods, disk space management becomes highly efficient.

5. Practical Solutions for Low Disk Space

Low disk space can slow the system and cause installation failures. This section explains practical solutions.

Delete Unnecessary Files and Directories

Remove Temporary Files

sudo rm -rf /tmp/*

Empty Trash

rm -rf ~/.local/share/Trash/*

Remove Unnecessary Packages and Cache

sudo apt-get autoremove
sudo apt-get clean

Find and Delete Large Files

find / -type f -size +100M

Check Directory Usage

du -ah /path/to/directory | sort -rh | head -n 10

Manage Log Files

sudo journalctl --vacuum-size=50M
sudo nano /etc/logrotate.conf

Monitor Disk Usage Regularly

#!/bin/bash
df -h > ~/disk_usage_report.txt

Summary

By removing unnecessary files and regularly monitoring usage, disk space issues can be avoided.

6. FAQ

Q1: What is the difference between df and du?

A:

  • df shows overall file system usage.
  • du shows usage per directory or file.

Q2: How can I find out why disk usage suddenly increased?

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

Q3: Why doesn’t deleting files free up space?

lsof | grep deleted
kill -9 <process ID>

Q4: How can I quickly find directories consuming space?

du -ah /path/to/directory | sort -rh | head -n 10

Q5: How can I regularly monitor disk usage?

#!/bin/bash
df -h > ~/disk_usage_report.txt

Q6: How do I prevent log files from growing too large?

sudo nano /etc/logrotate.conf

Q7: How can I prevent disk shortages?

  • Regular checks with df and du
  • Remove unnecessary files
  • Use automated tools

7. Conclusion

Disk space management in Ubuntu is essential for maintaining stability and performance. This article covered everything from basic commands to troubleshooting and optimization strategies.

Key Takeaways

  • Use df for overall system checks.
  • Use du to drill down into specific directories.
  • Combine GUI and CLI tools for maximum efficiency.

Final Advice

  • Monitor disk space regularly.
  • Use the right tools for each task.
  • Follow the steps in this guide when issues arise.

Closing Note

Ubuntu disk management may seem complex, but by following the steps introduced in this article, anyone can manage it with confidence. Use this knowledge to maintain a smooth and efficient system.