The following compress and purge approaches handle log files (in this case, from Tomcat) - they are two variations on the same theme.
Compress and Purge
xargs -0r ensures empty input pipes are ignored. Or use
0 in both
xargs -0r is a safeguard against files names containing spaces. Any lists (of files, in this case) use the null character as their item separator.
The script needs to be scheduled in crontab.
The logrotate file
/etc/logrotate.d/tomcat contains this:
180 means 180 old files will be kept.
copytruncate directive allows a process to continue writing to a rotated file.
Make sure logrotate is actually being executed. Various ways to do this. See notes here.
“For most daemon processes, logs should be rotated by the root user. In most cases, logrotate is invoked from a script in the /etc/cron.daily/ directory. If one does not exist, create a script that resembles the following…”