Optimizing Disk Space on Linux and program to run ONE TIME a MONTH
Written by Francisco Gonzalez
Published on: 2024-02-05
Clean your linux disk with docker environment
Maintaining sufficient disk space is crucial for the smooth operation of any Linux system. Over time, unused files, logs, and temporary data can accumulate, leading to inefficient disk usage. This tutorial provides a step-by-step guide on how to automate disk space optimization on a Linux system, ensuring your system remains efficient and clutter-free.
- Basic knowledge of Linux commands
- Root access to the Linux system
- Docker installed (if docker cleanup is required)
The provided script automates the process of cleaning up unnecessary files and optimizing disk space. It includes several key components:
- Display Current Disk Usage: The script starts by displaying the current disk usage using the
- Remove Specific Directories: It removes user-specified directories. Adjust
/path/to/directoryto the directories you want to delete.
- Clean Temporary Files and Cache: It cleans up temporary files and cache using
apt-get autoremove. This is particularly useful for Debian-based distributions.
- Reduce System Log Size: Reduces the size of system logs, keeping logs from the last three days using
- Clean Unused Docker Images: For systems using Docker, it removes unused Docker images and containers with
docker system prune -a -f.
- Display Disk Usage After Cleanup: Again shows the disk usage after the cleanup process.
- Logging Activity: The script logs the cleanup activity in a specified log file.
# Display current disk usage
echo "Disk space usage before cleanup:"
# Remove specific directories (adjust according to your needs)
rm -rf /path/to/directory
# Clean temporary files and cache (adjust as needed)
# This is just an example, make sure to adapt it to your needs
apt-get clean && apt-get autoremove
# Reduce the size of system logs.
sudo journalctl --vacuum-time=3d
# Clean up unused Docker images
docker system prune -a -f
# Display disk usage after cleanup
echo "Disk space usage after cleanup:"
# Activity log
echo "Cleanup completed on: $(date)" >> /path/to/logfile.log
1. Create the Script
- Open a text editor and paste the provided script.
touch disk_cleanup.sh && nano disk_cleanup.sh
- Modify the paths and commands according to your requirements.
- Save the file as
2. Make the Script Executable
Run the following command in the terminal:
chmod +x disk_cleanup.sh
3. Testing the Script
Before automating, test the script:
4. Automate the Script (Monthly Cleanup)
- Edit the crontab file for the root user:
sudo crontab -e
- Add the following line to run the script monthly:
0 0 1 * * /path/to/disk_cleanup.sh
- Save and exit the editor.
By following this guide, you’ve successfully set up a monthly disk space optimization routine for your Linux system. This automation ensures that your system remains clean and efficient, preventing potential issues related to disk space shortages.
Remember to periodically review and adjust the script to meet your evolving system needs.