As you use your Linux system, files and logs accumulate over time. These files can include temporary data, cached files, and outdated logs. Left unmanaged, they take up valuable disk space and can slow down your system. Checking and cleaning your system is essential to prevent it from filling up.

Reclaiming disk space in Linux can be done using standard system commands and tools. Some methods work across distributions, while others are specific to distributions like Ubuntu, Debian, or Fedora. It's important to know which method works best for your system and follow appropriate steps to remove unnecessary files.

Clearing disk space involves identifying large files, cleaning up system logs, and removing unused applications. Understanding how to check disk usage and remove unnecessary files will ensure your system performs efficiently. Regular cleanup is key to maintaining free disk space on Linux.

Steps to clear disk space on Linux:

  1. Check the disk usage for all mounted drives and file systems.
    $ df -h
    Filesystem      Size  Used Avail Use% Mounted on
    tmpfs           391M  1.9M  389M   1% /run
    /dev/sda3        20G  7.2G   11G  40% /

    Use the df command to show the disk space usage of each mounted file system. Pay attention to the percentage in the Use% column.

  2. Identify directories that consume significant space on each file system.
    $ du -h --max-depth=1 /
    2.0G    /var
    1.2G    /usr
    500M    /home

    The du command helps identify which directories consume the most space. Focus on directories like var and home.

  3. Locate large files and temporary files that are safe to delete.
    $ sudo find / -type f -size +500M
    /data/largefile.img
    /var/log/biglog.log

    Use the find command to locate large files, such as those above 500MB, that may be taking up valuable disk space.

  4. Delete unnecessary files from directories such as Downloads and Trash.
    $ rm -rf ~/Downloads/*
    $ rm -rf ~/.local/share/Trash/files/*

    Empty the Downloads and Trash directories regularly, as they often contain unneeded files.

  5. Remove unused packages and dependencies that are no longer required.
    $ sudo apt autoremove
    $ sudo yum autoremove

    Run the autoremove command to remove unused dependencies and outdated packages. This works for both Ubuntu (apt) and Fedora (yum) based systems.

  6. Clear cached package installer files to free up space.
    $ sudo apt clean
    $ sudo yum clean all

    This clears the package cache from the system, freeing up additional space.

  7. Purge old system logs and journal entries.
    $ sudo journalctl --vacuum-time=7d

    System logs can accumulate quickly. Use the journalctl command to remove logs older than 7 days.

  8. Remove user-specific application data if it's not needed.
    $ du -sh ~/.cache
    500M    /home/user/.cache
    $ rm -rf ~/.cache/*

    User-specific caches, stored in the .cache directory, can grow large over time. Deleting them frees up space without affecting system operation.

  9. Clean up directories left behind by deleted users.
    $ sudo rm -rf /home/olduser/

    Check for home directories of users no longer present on the system. These can be safely deleted to free up space.

Discuss the article:

Comment anonymously. Login not required.