Must-Read for Beginners! How to Check Disk Space in Ubuntu | Comprehensive Guide to Commands and GUI Tools

目次

1. Introduction

Ubuntu is widely used not only for personal use but also for server operations due to its lightweight nature and stability. However, as you continue using it, disk space will inevitably start to run low. A lack of disk space can lead to system performance degradation and failures when installing new software.

In this article, we will explain in detail how to check and properly manage disk space in Ubuntu. We will cover the usage of CLI (Command Line Interface) tools such as the df and du commands, as well as the GUI tool “Disk Usage Analyzer” that allows you to visualize disk usage. We provide step-by-step instructions with examples to make it easy for beginners to follow, so feel free to read on.

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

To check the overall disk usage in Ubuntu, use the df command. This command displays disk usage and available space for each file system in a list format. Below, we will explain the basics and some advanced use cases.

What is the df Command?

df stands for “disk free” and is a command used in Linux and Unix-based operating systems to check disk usage and available space. It is simple and fast, allowing users to instantly grasp the system’s disk status.

Basic Usage

The most common usage of the df command is as follows:

df -h
  • -h option
    Displays output in a human-readable format (with units). Instead of raw numbers like “1024000,” it will show sizes in formats like “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: The type of file system used (e.g., ext4, tmpfs).
  • Size: The total size of the file system.
  • Used: The amount of disk space currently in use.
  • Avail: The available free space.
  • Use%: The percentage of disk space used.
  • Mounted on: The mount point of the file system.

Advanced Usage

Displaying Only a Specific File System

Using the -T option, you can display file system types, and you can also filter by a specific type.

df -T ext4

This command will display information only for file systems formatted as ext4.

Checking a Specific Mount Point

If you want to check only the disk space for a specific mount point (e.g., /home), use the following command:

df -h /home

This will display the total and available disk space allocated to the /home directory.

Troubleshooting Tips

  • When Disk Space Runs Out
    Use the df command to identify file systems with 100% usage. Then, take steps such as deleting unnecessary files.
  • df Command Output Does Not Update
    If free space is not updated after deleting files, a process may still be using them. Use the lsof command to identify the process and take appropriate action.
lsof | grep deleted

Summary

The df command is a useful tool for quickly checking overall disk usage in Ubuntu. The -h option makes the output more readable, making it accessible even for beginners. By mastering the basics and advanced uses covered in this section, you can manage your system’s disk space more efficiently.

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

Checking overall disk usage is not enough to determine which directories or files are consuming the most space. In such cases, the du command allows you to analyze disk usage on a per-directory or per-file basis. This section explains the basics and advanced use cases of the du command.

What is the du Command?

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

Basic Usage

A simple usage example of the du command is:

du -sh /path/to/directory
  • -s option
    Displays only the total size of the specified directory.
  • -h option
    Outputs in a human-readable format (KB, MB, GB).

Example Output

5.2G    /home/user/Documents

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

Checking Detailed Usage

Displaying Disk Usage for Subdirectories

To check the disk usage of subdirectories within a specific directory, use the following command:

du -h /path/to/directory/*

Example

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

This makes it easy to identify which subdirectory is using the most space.

Advanced Usage

Sorting by Size

To sort directories and files by disk usage in descending order, use the sort command:

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

Example

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

Summary

The du command is a powerful tool for analyzing disk usage at the directory and file level. Combining it with commands like find and sort can help you quickly identify and resolve disk space issues.

4. How to Check Disk Usage Visually Using GUI (Disk Usage Analyzer)

Besides using command-line tools, you can also check disk usage visually using a GUI (Graphical User Interface). In Ubuntu, the “Disk Usage Analyzer” tool allows you to analyze disk usage graphically. This section explains the features, installation method, and basic usage of Disk Usage Analyzer.

What is Disk Usage Analyzer?

Disk Usage Analyzer is a disk management tool included by default in Ubuntu. It provides a graphical representation of disk usage, such as pie charts and bar graphs, making it easy to identify which directories and files are consuming the most space.

How to Install

In most Ubuntu versions, Disk Usage Analyzer (baobab) is pre-installed. If it is not installed, you can install it using the following command:

sudo apt update
sudo apt install baobab

After installation, you can launch it by searching for “Disk Usage Analyzer” in the application menu.

Basic Usage

1. Launching the Tool

There are two ways to launch Disk Usage Analyzer:

  1. Search for “Disk Usage Analyzer” in the application menu and open it.
  2. Launch it from the terminal with the following command:
baobab

2. Selecting a Directory to Scan

When you launch the tool, you will see the following options:

  • Scan Home Folder
    Scans the entire home directory by default.
  • Select a Specific Folder
    Click the “Select Folder” button to choose a directory to analyze.
  • Scan Remote Disks
    Check the disk usage of network storage or remote servers.

3. Checking Disk Usage

Once the scan is complete, you will see:

  • Graphical Representation
    Disk usage is displayed in a pie chart or bar graph format.
  • Detailed List
    You can check the disk usage, available space, and the number of files in each directory.

Useful Features

1. Identifying Large Files

You can expand directories to quickly locate large files or folders, helping you decide what to delete.

2. Scanning Network Drives

Disk Usage Analyzer supports scanning remote servers and network storage (e.g., NFS, SMB), which is useful for checking disk usage on external drives.

3. Exporting Scan Results

You can export scan results for later reference or to share with team members.

Pros and Cons

Pros

  • Easy to Understand: Graphs provide a clear visual representation of disk usage.
  • Beginner-Friendly: No command-line knowledge required; everything can be done through clicks.
  • Supports Network Disks: Can scan remote storage devices.

Cons

  • Scanning Can Be Slow: Large directories may take a long time to scan.
  • Limited Customization: Compared to CLI tools, there are fewer advanced filtering and sorting options.

Summary

Disk Usage Analyzer is a useful tool for users of all levels. The graphical display makes it easy to identify which directories or files are taking up the most disk space. By combining it with CLI tools, you can efficiently manage disk space.

5. How to Free Up Disk Space When Running Low

When disk space runs low, the system may slow down or fail to install new software. This section explains practical ways to free up disk space.

Deleting Unnecessary Files and Directories

1. Removing Temporary Files

Temporary files can accumulate and take up space. Use the following command to delete them:

sudo rm -rf /tmp/*

2. Emptying the Trash

Deleted files remain in the trash, taking up space. Use this command to empty it:

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

Removing Unused Packages and Cache

1. Removing Unused Packages

Use this command to remove unnecessary packages:

sudo apt-get autoremove

2. Clearing Package Cache

The following command removes cached installation files:

sudo apt-get clean

Finding and Deleting Large Files

1. Searching for Large Files

Use the following command to list files larger than 100MB:

find / -type f -size +100M

Managing Log Files

1. Deleting Old Logs

To shrink log files, use:

sudo journalctl --vacuum-size=50M

Regular Monitoring of Disk Usage

1. Using GUI for Monitoring

Regularly check disk usage using “Disk Usage Analyzer” or du command.

2. Automating Monitoring

Use a script to automatically check disk usage:

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

Summary

By following these steps, you can free up disk space and prevent future issues. Regularly checking and cleaning your disk will help maintain system performance.

6. FAQ (Frequently Asked Questions)

Q1: What is the difference between df and du commands?

  • df command: Displays overall disk usage per file system.
  • du command: Shows disk usage for specific directories and files.

Q2: How do I identify what is using up my disk space?

Use the following command:

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

Q3: Why is my deleted file not freeing up space?

Use this command to check if a process is still using the file:

lsof | grep deleted

7. Conclusion

Managing disk space in Ubuntu is crucial for system stability. By using the methods outlined in this article, you can efficiently check, analyze, and free up disk space. Regular maintenance will keep your system running smoothly.

侍エンジニア塾