A comprehensive Bash script for automating common system maintenance tasks including cache cleanup, duplicate file detection, and bulk file renaming.
- Features
- Requirements
- Installation
- Usage
- Configuration
- What It Does
- Safety Features
- Examples
- Troubleshooting
- Contributing
- License
✨ Three main functions:
- System Cleanup - Removes cache files and old logs to free up disk space
- Duplicate Finder - Identifies duplicate files based on content (SHA256 hash)
- Bulk Renamer - Standardizes filenames to lowercase with consistent formatting
🛡️ Safety First:
- Dry-run mode by default (preview changes before applying)
- Comprehensive error handling
- Automatic cleanup of temporary files
- Collision detection for rename operations
- Safe handling of filenames with special characters
- OS: Linux or macOS
- Bash: Version 4.0 or higher
- Utilities:
sha256sum(orshasumon macOS)statfind- Standard GNU coreutils
Check your Bash version:
bash --version-
Download the script:
curl -O https://raw.githubusercontent.com/rustybladez/clean-my-system/refs/heads/main/cleanup.sh # OR wget https://raw.githubusercontent.com/rustybladez/clean-my-system/refs/heads/main/cleanup.sh -
Make it executable:
chmod +x cleanup.sh
-
Optional - Move to your PATH:
sudo mv cleanup.sh /usr/local/bin/cleanup
Dry-run mode (safe preview - recommended first time):
./cleanup.shActually execute the operations:
- Edit the script and change
DRY_RUN=truetoDRY_RUN=false - Run the script:
./cleanup.sh
sudo ./cleanup.shNote: Only run with sudo if you need to clean system logs. The script will skip system-wide operations if not running as root.
Edit these variables at the top of the script:
# Directory to search for duplicate files
DUPLICATE_DIR="${HOME}/Documents"
# Directory where bulk renaming will occur
TARGET_DIR="${HOME}"
# Delete log files older than this many days (requires root)
LOG_DAYS=7
# Preview mode - set to false to actually execute operations
DRY_RUN=trueFor conservative cleanup:
TARGET_DIR="${HOME}/Downloads"
DUPLICATE_DIR="${HOME}/Downloads"
LOG_DAYS=30
DRY_RUN=trueFor aggressive cleanup:
TARGET_DIR="${HOME}"
DUPLICATE_DIR="${HOME}"
LOG_DAYS=7
DRY_RUN=false # Be careful!Removes temporary and cache files to free up disk space:
User-level cleanup (no sudo required):
~/.cache/*- General application cache~/.mozilla/firefox/*/cache2/*- Firefox browser cache~/.config/google-chrome/Default/Cache/*- Chrome browser cache
System-level cleanup (requires sudo):
/var/log/*.log- Log files older thanLOG_DAYS
What it DOESN'T touch:
/tmp/*- System temporary directory (too risky)- Important configuration files
- User documents or data
Intelligently identifies duplicate files:
How it works:
- Scans
DUPLICATE_DIRfor files larger than 1MB - Groups files by size
- Calculates SHA256 hashes only for files with matching sizes (efficient!)
- Reports groups of files with identical content
Example output:
[2024-02-13 10:30:15] == Finding duplicates in /home/user/Documents ==
a1b2c3d4... /home/user/Documents/photo1.jpg
a1b2c3d4... /home/user/Documents/backup/photo1.jpg
e5f6g7h8... /home/user/Documents/report.pdf
e5f6g7h8... /home/user/Documents/final_report.pdf
[2024-02-13 10:30:45] Total duplicate groups: 2
Note: The script only identifies duplicates - it doesn't delete them automatically. You must manually review and delete.
Standardizes filenames in TARGET_DIR:
Transformations applied:
- Converts to lowercase:
MyFile.txt→myfile.txt - Replaces spaces with hyphens:
my file.txt→my-file.txt - Removes special characters:
my#file!.txt→myfile.txt - Preserves: letters, numbers, dots, underscores, hyphens
Examples:
Before After
──────────────────────────────────────────────────
"My Vacation Photos 2024.jpg" my-vacation-photos-2024.jpg
"Project_Final (v2).docx" project_final-v2.docx
"Budget $$ 2024.xlsx" budget-2024.xlsx
Safety features:
- Detects naming collisions
- Uses
-nflag to never overwrite existing files - Reports statistics on completion
Enabled by default - shows what would happen without making changes:
[DRY] rm -rf /home/user/.cache/*
[DRY] mv "My File.txt" "my-file.txt"
set -euo pipefail- Exits on any error- Directory existence checks
- File accessibility validation
TEMP_DIR=$(mktemp -d)
trap 'rm -rf "$TEMP_DIR"' EXITAutomatically cleans up temp files even if the script crashes.
Uses null-delimited lists (-print0 and -d '') to safely handle:
- Filenames with spaces
- Filenames with newlines
- Filenames with special characters
Checks if target filename exists before renaming:
WARNING: Collision detected - myfile.txt already exists, skipping MyFile.txt
# Edit the script
TARGET_DIR="$HOME/Downloads"
DUPLICATE_DIR="$HOME/Downloads"
DRY_RUN=true
# Run in dry-run mode first
./cleanup.sh
# Review output, then disable dry-run
DRY_RUN=false
./cleanup.sh# Edit the script
DUPLICATE_DIR="$HOME/Pictures"
DRY_RUN=true
# Run only the duplicate finder
# Comment out the other functions in main()# Edit the script
TARGET_DIR="$HOME/Documents/Reports"
DRY_RUN=true
# Preview changes
./cleanup.sh
# Apply changes
DRY_RUN=false
./cleanup.sh# Run with sudo for system logs
sudo DRY_RUN=false ./cleanup.shProblem: Script can't access certain directories
Solution:
# Run with sudo (be careful!)
sudo ./cleanup.sh
# OR modify TARGET_DIR to directories you own
TARGET_DIR="$HOME/MyFolder"Problem: Different stat syntax on macOS vs Linux
Solution: The script auto-detects and uses the correct syntax. If it still fails:
# Install GNU coreutils on macOS
brew install coreutilsProblem: Files are smaller than 1MB
Solution: Edit the script and change the size filter:
# In find_duplicates function, change:
find "$DUPLICATE_DIR" -type f -size +1M
# To:
find "$DUPLICATE_DIR" -type f -size +100k # 100KB minimumProblem: Extension was removed due to special characters
Solution: The script preserves dots (.) by default. If extensions are missing, check the original filename for special characters.
Problem: DRY_RUN is still enabled
Solution:
# Check the DRY_RUN variable
grep "DRY_RUN=" cleanup.sh
# Change to:
DRY_RUN=false-
Always run in dry-run mode first
DRY_RUN=true
-
Start with a small test directory
TARGET_DIR="$HOME/test_folder" -
Backup important data before bulk operations
cp -r ~/Documents ~/Documents.backup
-
Review duplicate file lists carefully
- Open the files to verify they're actually duplicates
- Keep the newest or highest quality version
-
Check disk space before cleanup
df -h ~ -
Schedule regular cleanups (optional)
# Add to crontab (weekly cleanup) 0 2 * * 0 /path/to/cleanup.sh
[2024-02-13 10:00:00] Starting cleanup script (DRY_RUN=false)
[2024-02-13 10:00:01] == System cleanup ==
[2024-02-13 10:00:05] == Finding duplicates in /home/user/Documents ==
[2024-02-13 10:00:45] Total duplicate groups: 3
[2024-02-13 10:00:46] == Bulk renaming in /home/user ==
[2024-02-13 10:01:00] Renamed 42 files, 2 collisions avoided
[2024-02-13 10:01:00] Done.
[DRY] rm -rf /home/user/.cache/*
[DRY] mv "File Name.txt" "file-name.txt"
Each [DRY] line shows what would be executed.
- Large directories: May take several minutes for 100,000+ files
- Hash calculation: ~1-2 seconds per GB of duplicate file data
- Memory usage: Minimal - processes files iteratively
- Disk I/O: Intensive during duplicate detection phase
- Never run untrusted scripts as root
- Review the code before executing
- Test on non-critical data first
- Keep backups of important files
- Use dry-run mode before any destructive operation
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Test thoroughly with dry-run mode
- Submit a pull request
For issues, questions, or suggestions:
- Open an issue on GitHub
- Check existing issues for solutions
- Review the troubleshooting section above