Skip to content

Instantly share code, notes, and snippets.

@serg-kovalev
Last active January 18, 2024 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save serg-kovalev/a8502ba876f27b4310e8fae5e0032daa to your computer and use it in GitHub Desktop.
Save serg-kovalev/a8502ba876f27b4310e8fae5e0032daa to your computer and use it in GitHub Desktop.
Truncate log files (Linux/Macos), requires bash
#!/bin/sh
### you can add it to cron and execute on hourly/daily/weekly/monthly basis
### this script logs into cleanup_logs.log file
find /home /var/log -type f -name "*.log" -exec bash -c 'size=$(du -m "{}" | cut -f1); [ "$size" -gt 1 ] && truncate -s 1M "{}" && echo "{}"' \; >> cleanup_logs.log
echo "last started at $(date)" >> cleanup_logs.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment