Smart Linux System Cleaner ๐งนโจ

A comprehensive, safe, and feature-rich Bash script to clean up and maintain your Linux system. Works across multiple Linux distributions with advanced safety features and extensive cleaning capabilities.
โจ Features
Package Management
- ๐ฆ Updates package repositories
- โฌ๏ธ Upgrades system packages
- ๐๏ธ Removes orphaned packages
- ๐พ Cleans package caches
- ๐ง Manages old kernels safely
- ๐ฑ Supports Flatpak and Snap packages
System Cleaning
- ๐๏ธ Cleans systemd journal logs (time and size based)
- ๐ Removes old temporary files
- ๐ผ๏ธ Cleans thumbnail caches
- ๐๏ธ Empties user trash
- ๐ Manages application logs
- ๐๏ธ Removes old compressed system logs
Browser Cleaning
- ๐ Firefox cache cleaning
- ๐ Chrome/Chromium cache cleaning
- ๐ Optional history cleaning (disabled by default)
Desktop Environment
- ๐ Clears recent documents (optional)
- ๐ Clears clipboard history (optional)
- ๐จ Cleans icon cache
Docker Support
- ๐ณ Docker system pruning
- ๐ฆ Optional volume pruning (with confirmation)
- ๐๏ธ Optional builder cache cleaning
Safety Features
- ๐ Dry run mode to preview changes
- ๐ Space tracking shows freed space per operation
- โ ๏ธ Interactive confirmations for dangerous operations
- ๐ก๏ธ Conservative defaults to prevent data loss
- ๐ Protects current kernel from deletion
๐ฅ๏ธ Supported Distributions
- Debian/Ubuntu (apt)
- Fedora (dnf)
- RHEL/CentOS (yum)
- openSUSE (zypper)
- Arch Linux (pacman)
- NixOS (nix)
๐ Installation
Clone the repository:
git clone https://github.com/turtlecute33/linux-system-cleaner.git
cd linux-system-cleaner
Make the script executable:
๐ Usage
Basic Usage
Run with root privileges using your preferred method:
# Using sudo
sudo ./clean-script.sh
# Using doas
doas ./clean-script.sh
# As root
./clean-script.sh
Dry Run Mode (Recommended First Time)
Preview what will be cleaned without actually deleting anything:
# Edit the script and set: DRY_RUN=true
sudo ./clean-script.sh
Custom Configuration
Edit the configuration section at the top of the script to customize behavior:
# Example: Enable browser cache cleaning
CLEAN_BROWSER_CACHE=true
# Example: More aggressive temp file cleaning
TMP_FILE_MAX_AGE_DAYS=3
# Example: Keep only 1 old kernel
KEEP_KERNELS=1
โ๏ธ Configuration Options
Basic Settings
KEEP_KERNELS=2 # Number of old kernels to keep
JOURNAL_VACUUM_TIME="3d" # Max age for journal logs
JOURNAL_VACUUM_SIZE="500M" # Max size for journal logs
TMP_FILE_MAX_AGE_DAYS=7 # Age threshold for temp files
USER_CACHE_MAX_AGE_DAYS=14 # Age threshold for user cache
Cleaning Options
CLEAN_USER_CACHE_FILES=true # Clean general cache files
CLEAN_THUMBNAILS=true # Clean thumbnail cache
CLEAN_TRASH=true # Empty trash
CLEAN_BROWSER_CACHE=false # Clean browser caches
CLEAN_BROWSER_HISTORY=false # Clean browser history (โ ๏ธ)
CLEAN_RECENT_DOCUMENTS=false # Clear recent documents
CLEAN_CLIPBOARD_HISTORY=false # Clear clipboard history
CLEAN_APPLICATION_LOGS=true # Clean app logs
CLEAN_DOCKER=true # Enable Docker cleanup
CLEAN_DOCKER_VOLUMES=false # Prune Docker volumes (โ ๏ธ)
Advanced Options
AGGRESSIVE_PKG_CACHE_CLEAN=false # More aggressive cache cleaning
DRY_RUN=false # Preview mode (no deletions)
๐ก๏ธ Safety Features
What Gets Protected
- โ
Current running kernel
- โ
User documents and files
- โ
Application configurations
- โ
Database files
- โ
Active Docker containers/images
- โ
Browser bookmarks and passwords
What Gets Cleaned (Safely)
- โ
Package manager caches
- โ
Old system logs
- โ
Thumbnail caches
- โ
Temporary files (older than threshold)
- โ
Trash contents
- โ
Browser caches (if enabled)
- โ
Old kernels (keeping recent ones)
Dangerous Operations (Require Confirmation)
- โ ๏ธ Browser history deletion
- โ ๏ธ Docker volume pruning
- โ ๏ธ Docker builder cache cleaning
๐ Output Example
###############################################################
## ๐ Smart Linux System Cleaner v2.0.0 ๐ ##
###############################################################
>>> Configuration Summary:
:: Keep Kernels: 2
:: Journal Vacuum: 3d / 500M
:: Temp Files Age: 7 days
:: User Cache Age: 14 days
:: Dry Run Mode: false
>>> Initial Disk Usage
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 900G 40G 815G 5% /
>>> Detected Package Manager: apt
>>> [1/13] Updating package repositories...
>>> [2/13] Upgrading system packages...
>>> [3/13] Removing orphaned/unneeded packages...
โ OK: Freed 128MB
>>> [4/13] Cleaning package cache...
โ OK: Freed 2GB
...
>>> Final Disk Usage
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 900G 37G 818G 5% /
###############################################################
## โ
System Cleanup Completed Successfully! โ
##
###############################################################
โ OK: Total Space Freed: 3GB
โ OK: Script Duration: 45 seconds
๐ง Privilege Escalation Support
The script automatically detects and uses the available privilege escalation method:
- root: Runs commands directly
- sudo: Standard Linux privilege escalation
- doas: OpenBSD-style privilege escalation (Alpine, Void, etc.)
โก Quick Tips
Safe First Run
# 1. Preview what will be cleaned
# Edit script: DRY_RUN=true
sudo ./clean-script.sh
# 2. Review the output
# 3. Adjust configuration if needed
# 4. Run for real
# Edit script: DRY_RUN=false
sudo ./clean-script.sh
Regular Maintenance
# Run monthly with default settings
sudo ./clean-script.sh
Aggressive Cleaning
# Edit these settings in the script:
TMP_FILE_MAX_AGE_DAYS=3
USER_CACHE_MAX_AGE_DAYS=7
CLEAN_BROWSER_CACHE=true
AGGRESSIVE_PKG_CACHE_CLEAN=true
๐ Troubleshooting
"Command not found" errors
Some optional tools may not be installed:
paccache: Install pacman-contrib on Arch
package-cleanup: Install yum-utils on RHEL/CentOS
Script requires root privileges
# Make sure to run with sudo, doas, or as root
sudo ./clean-script.sh
Nothing gets deleted
Check if DRY_RUN=true is set in the configuration.
โ ๏ธ Important Notes
Before Running
- ๐ด Backup important data before first run
- ๐ด Test with dry run mode first
- ๐ด Review configuration options carefully
- ๐ด Understand what each option does
During Execution
- โ
Read confirmation prompts carefully
- โ
Don't interrupt kernel removal operations
- โ
Watch for warnings and errors
After Running
- โ
Review freed space statistics
- โ
Verify system still boots properly
- โ
Check that applications work correctly
๐ฏ Recommended Settings
For Desktop Users
KEEP_KERNELS=2
USER_CACHE_MAX_AGE_DAYS=14
CLEAN_BROWSER_CACHE=false # Enable if needed
CLEAN_DOCKER=false # Unless you use Docker
For Servers
KEEP_KERNELS=2
JOURNAL_VACUUM_TIME="7d" # Keep more logs
CLEAN_BROWSER_CACHE=false # No browsers on servers
CLEAN_DOCKER=true # If running containers
For Privacy-Focused Users
CLEAN_BROWSER_CACHE=true
CLEAN_RECENT_DOCUMENTS=true
CLEAN_CLIPBOARD_HISTORY=true
# Keep CLEAN_BROWSER_HISTORY=false unless you're sure
๐ค Contributing
Contributions are welcome! Please:
- Test changes thoroughly
- Maintain backward compatibility
- Document new features
- Follow existing code style
- Add safety checks for new operations
๐ License
This project is open source. Feel free to modify and distribute as needed.
๐ Acknowledgments
Inspired by various Linux maintenance tools and best practices from the community.
๐ Support
If you encounter issues:
- Check the troubleshooting section
- Review your configuration
- Test with dry run mode
- Open an issue on GitHub with details
Happy cleaning! ๐งนโจ
Made with โค๏ธ for the Linux community