All Posts

How to optimize disk space on Ubuntu servers

Posted by Mike on May 23rd, 2023
How to optimize disk space on Ubuntu servers

Disk space is a valuable resource on any Linux server, and it's crucial to manage it efficiently to ensure optimal performance and prevent system slowdowns or crashes. In this tutorial, we will explore how to detect and remove large files and folders when your disk space is full on Ubuntu-based Linux servers. By following these steps, you can regain valuable disk space and maintain a healthy server environment.

Step 1: Identifying Large Files and Folders:

The first step in managing disk space is to identify the large files and folders that are consuming the most space. Ubuntu provides several command-line tools to accomplish this task effectively. Open a terminal and execute the following commands:

cd / - This command changes the directory to the root directory.
sudo du -hsx * | sort -rh | head -n 20 - This command lists the top 20 largest files and folders in the current directory, sorted by size.
The output will display the sizes of the files and folders in human-readable format (e.g., 10G for 10 gigabytes). Analyze the output and identify the ones consuming excessive disk space.

Step 2: Removing Unnecessary Files:

Once you've identified large files or folders that are no longer needed, it's time to remove them. However, exercise caution while deleting files, as some may be critical to the system or other applications. To remove files and folders, use the rm command:

sudo rm -rf /path/to/file_or_folder - Replace /path/to/file_or_folder with the actual path of the file or folder you want to delete. The -r flag is used to remove directories recursively, and -f is used to force deletion without confirmation.
Double-check the paths before executing the command to avoid accidental deletion of important data.

Step 3: Clearing System Logs:

System logs can accumulate over time and occupy a significant amount of disk space. It's recommended to periodically clear these logs to free up storage. Ubuntu uses the logrotate utility to manage logs. To clear logs manually, use the following command:

sudo rm /var/log/*/*.log - This command removes log files from the /var/log directory. Note that this operation requires administrative privileges.

Step 4: Cleaning Package Cache:

Ubuntu uses the Advanced Packaging Tool (APT) to manage software packages. APT stores downloaded package files in the cache directory, which can consume a considerable amount of disk space. To clean the package cache, run the following commands:

sudo apt-get clean - This command clears the local repository of retrieved package files.
sudo apt-get autoclean - This command removes only the obsolete package files from the cache directory.

Step 5: Clearing the Trash and Temporary Files:

On a server without a user interface, you can manually clear the trash and temporary files to free up disk space. These files can accumulate over time and consume valuable storage. Follow these steps to remove them:

  1. Run the following command to navigate to the user's home directory:
    cd ~
    
  2. Execute the following command to view the contents of the trash directory:
    ls -A .local/share/Trash/
    
    This command lists the files in the trash folder.
  3. To permanently remove files from the trash, use the following command:
    rm -rf .local/share/Trash/*
    
    This command recursively deletes all files in the trash folder.
  4. Additionally, temporary files created by applications can consume disk space. Clear them using the following command:
    sudo rm -rf /tmp/*
    
    Note that this action removes all files and directories in the temporary directory and may require administrator privileges.

By clearing the trash and removing temporary files, you can reclaim significant disk space on your Ubuntu-based Linux server, ensuring efficient resource utilization.

Efficiently managing disk space is vital for the smooth operation of Ubuntu-based Linux servers. By following the steps outlined in this tutorial, you can easily detect and remove large files and folders that are occupying excessive disk space. Regular maintenance and periodic cleaning will help ensure optimal performance and prevent storage-related issues on your server.

Server Management & Security doesn't have to be a full time job.

ServerAuth provides a whole host of management tools, from controlling who can access your server, to managing your website deployments. And with an ever-growing suite of tools you'll always be one step ahead!

Server Management Software Screenshot
ServerAuth
Server Management & SSH Security Software
 on X (Twitter)
Copyright © Peakstone Ltd
Registered in England & Wales No. 13996293
All Rights Reserved.
Solutions
Resources
Support
Customers
ServerAuth
The Legal Bits