How to Check and Reduce Disk Usage in Linux?

Disk usage is an important aspect of system management, and understanding how to check and reduce it can help maintain an efficient and responsive server. This guide will explain how to check disk usage in Linux using various commands and methods and how to reduce unnecessary disk space consumption.

Checking Disk Usage in Linux

Several commands are available in Linux to check disk space and identify the usage pattern on your server. These commands can help you monitor the health of your system and spot any areas where disk space might be used inefficiently.

1. df Command – Display Disk Space Usage

The df command is commonly used to display the amount of disk space utilized and available on all mounted file systems. Here’s how you can use it:

  • Basic Usage:

    $ df

    Example Output:

    Filesystem 1K-blocks Used Available Use% Mounted on /dev/xvda1 15348700 5538860 9709592 37% /

  • Show All File Systems:

    $ df -a

    This will list all file systems, including special file systems like proc and sysfs.

  • Human-Readable Format:

    $ df -h

    This option displays the disk space in human-readable units (e.g., MB, GB).

    Example Output:

    Filesystem Size Used Avail Use% Mounted on /dev/xvda1 15G 5.3G 9.3G 37% /

  • Specific File System (e.g., /home):

    $ df -hT /home

    This shows the disk space usage for the /home directory specifically. Example Output:

    Filesystem Type Size Used Avail Use% Mounted on /dev/vdb1 ext4 296G 176G 105G 63% /home

  • In MB:

    $ df -m

  • Inode Usage:

    $ df -i

    This command shows inode usage, which can be useful when there are many small files on the system.

  • Filter Specific File Systems:

    $ df -t ext3

  • Exclude Specific File Systems:

    $ df -x ext3

2. du Command – Display Directory Disk Usage

The du command is used to find out how much disk space is used by specific directories and files.

  • Display Space Used by Directories:

    $ sudo du -h --max-depth=1

    This command will display disk usage for the directories in the current directory, up to a depth of 1.

3. For Btrfs File Systems:

If you are using a Btrfs file system, you can use:

$ btrfs fi df /device/

This shows disk space usage information for a Btrfs-based mount point or file system.

Reducing Disk Usage

Once you have identified areas where disk space is being used excessively, the next step is to reduce disk usage.

1. Navigate to the Root Directory:

cd /

2. Run the du Command:

Use the du command with a depth level of 1 to identify large directories:

sudo du -h --max-depth=1

3. Identify Large Directories:

Mark the directories that consume the most space.

4. Explore Large Directories:

Navigate into these directories to further investigate:

cd <directory>

5. List Large Files:

Use the ls command to identify large files in these directories:

ls -l

6. Delete Unwanted Files:

After identifying large files that are no longer needed, remove them:

rm <file_name>

7. Repeat Steps 2 to 6:

Continue the process for other directories and files until disk space usage is reduced to an acceptable level.

Conclusion

Managing disk usage effectively is crucial to maintaining a smooth and responsive Linux system. By using commands like df and du, you can identify where disk space is being used and take steps to reduce unnecessary consumption. Regular monitoring and cleanup are vital for ensuring optimal performance, especially for servers like COLO BIRD, where efficient resource management is essential for stability and speed. Keep track of disk space usage, and perform regular audits to avoid running into space issues in the future.