A Python utility script for monitoring and cleaning up disk space on Linux systems. This script identifies directories with high disk usage and helps remove old files to free up space.
- Disk Space Monitoring: Displays current disk usage for all mounted filesystems
- Automatic Detection: Identifies directories with 100% disk usage
- Smart Cleanup: Removes files older than 30 days
- Interactive Mode: Prompts for confirmation before deleting files
- Manual Mode: Allows cleaning specific directories manually
- Safety Checks: Verifies write permissions before attempting deletions
- Python 3.x
- Linux/Unix system with
dfcommand - Write permissions for directories you want to clean
-
Make the script executable:
chmod +x disk_cleanup.py
-
Run the script:
./disk_cleanup.py
or
python3 disk_cleanup.py
- Initial Scan: The script displays current disk space information using the
df -hcommand - Identify Full Directories: Automatically detects filesystems with 100% usage
- Cleaning Options:
- Automatic Mode: Automatically processes all full directories
- Manual Mode: Allows you to specify which directory to clean
- File Deletion: Removes files older than 30 days (files not modified in the last 30 days)
- Confirmation: Always asks for confirmation before deleting files
- Final Report: Shows disk space after cleanup
- Confirmation Prompts: Requires user confirmation before deleting files
- Permission Checks: Verifies write access before attempting deletions
- Error Handling: Gracefully handles file not found errors and permission issues
- File Age Filter: Only targets files older than 30 days
Disk space before cleanup:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 20G 20G 0 100% /
...
Directories with high disk usage:
/: 100.0%
Do you want to automatically clean the full directories? (y/n): y
- The script deletes files based on modification time (mtime)
- Files older than 30 days are considered for deletion
- The script will not delete files if the directory is read-only
- Always review the list of files marked for deletion before confirming
This script is provided as-is for personal use.