A robust Go utility for cleaning up old files in specified directories with configurable age thresholds and logging capabilities.
-
🗑️ Delete files older than N days
-
📝 Comprehensive logging (console and/or file)
-
🧪 Dry-run mode for testing
-
🛠️ Configurable via command-line arguments
-
📊 Helpful usage documentation
-
Go 1.19 or higher
-
Linux/Unix system (tested on Rocky Linux)
git clone https://github.com/rouve/go-cleanup.git cd go-cleanup
go build -o go-cleanup go-cleanup.go
sudo mv go-cleanup /usr/local/bin/
go install github.com/rouve/go-cleanup@latest
Check the Releases page for pre-built binaries.
go-cleanup --dir=/path/to/directory [options]
go-cleanup --dir=/tmp
go-cleanup --dir=/var/log --days=30
go-cleanup --dir=/backups --days=90 --dry-run
go-cleanup --dir=/data --days=14 --log=/var/log/go-cleanup.log
| Option | Description | Default |
|---|---|---|
--dir |
Directory to clean (required) | - |
--days |
Delete files older than N days | 7 |
--dry-run |
Simulate without deleting | false |
--log |
Path to log file | stdout |
--help |
Show help message | - |
Example cron entry to run daily at 2 AM:
0 2 * * * /usr/local/bin/go-cleanup --dir=/tmp --days=7 --log=/var/log/go-cleanup.log
For Webmin:
-
Go to "System" → "Scheduled Cron Jobs"
-
Add new command:
/usr/local/bin/go-cleanup --dir=/path --days=N -
Set desired schedule
Logs include timestamps and operation details. Example log output:
2023-07-20 14:30:45 - Starting cleanup in /tmp for files older than 7 days (2023-07-13 14:30:45) 2023-07-20 14:30:45 - Found candidate: tempfile.txt (modified: 2023-07-10 08:15:22) 2023-07-20 14:30:45 - Deleted: tempfile.txt 2023-07-20 14:30:45 - Cleanup complete. 1 files deleted (dry-run: false)
Pull requests are welcome! Please ensure:
-
Go code is properly formatted (
go fmt) -
New features include tests
-
Documentation is updated