Wednesday 30 May 2012

Linux: Delete old log files

Here's a simple shell command to delete old log(or any) files.

Syntax:

ls -t <list of files> | tail -n <count> | xargs rm -f

Usage:

 ls -t /data/www/myapp/shared/log/*.log.gz | tail -n +6 | xargs rm -f

This will remove all compressed log files leaving recent 5 backups.