skip to Main Content

How to remove log files in linux

How To Remove Log Files In Linux

Logrotate is used to remove log files, but sometimes, a simple bash script can be easier.

In my case, odoo writes log file in /var/log/odoo. This creates only small files that I don’t need it for long time. I will create a script to delete the log files that are older than 30 days.

As I want to run my script daily, I need to create script in directory /etc/cron.daily. Please run command below to create script file.

vim /etc/cron.daily/removelogs

Now we can add a simple script into file that we created

#!/bin/bash
# Remove files older than 30 days.
find /var/log/odoo/* -maxdepth 1 -mtime +30 -maxdepth 1 -exec rm {} \; > /var/log/logsdelete.log
# find: find files in location
# maxdepth: how far it should search. 1 is only top directory it's searching in. leave this out to 
# search everything below the folder.
# mtime: only display files older than +n days
# exec: execute rm on output
# >: write log file to .log

The comments in this script describes what the bash scrip do . It makes you easy to update the script base on your demands.

This Post Has 0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top
Search