Delete old log files

Logrotate is an utility in Ubuntu that allows package maintainers and admins to regulate how logfiles are stored, compressed and rolled for backups.

When creating lean virtual machine images, I found that I didn’t want to keep any logs.
I also didn’t want to go an reconfigure logrotate.

You can use the find command to get rid of all the backups, if you really don’t care about log backups.

/usr/bin/find /var/log/  \
    -type f \( -name "*\.log\.[0-9]*" -prune -o  \
        -name "*\.gz" -prune -o  \
        -name "*.[0-9]" -prune \)  \ 
    -exec rm -rf {} \;

If you must keep some log files based on their modifcation time, use the -mtime option.

find . -mtime 0   ( modified between now and 1 day ago )
find . -mtime 1   ( modified between 24 and 48 hours ago )
find . -mtime -1  ( modified less than 1 day ago )
find . -mtime +1  ( modified more than 48 hours ago )

This entry was posted in Bash and tagged . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *