How to clean log file and create a cron script to clean the log file ?

How to clean log file and create a cron script to clean the log file ?

November 18, 2020 / Nirav Shah

What is the log file in the server?

Server log files are a raw, unfiltered look at traffic to your site. They are text files stored on your web server. Every time any browser or user-agent, Google included, requests any resource-pages, images, javascript file, whatever-from your server, the server adds a line in the log.

Explaining a log file

11.222.333.44 – – [14/SEP/2020:11:01:28 –0600] “GET /blog/page-address.htm HTTP/1.1” 200 182 “-” “Mozilla/5.0 Chrome/60.0.3112.113”

  • On September 14, 2020, someone using Google Chrome tried to load The ‘200’ means the server found the file Page-address.htm is teeny, weighing in at 182 bytes.
  • The IP address of the client—the software that requested the file was—11.222.333.44. I put that last because for many reasons it’s not terribly helpful to us marketers.

Many times these log files occupy a considerable space in the server disk drive so we need to clean the log files periodically.

How to clean the log file?

Step 1

Navigate to /var/log there you will see many compressed file named as .tar.gz as an extension we need to clean this file

  • cd /var/log
  • sudo rm -rf *.gz

This will delete all the compressed files in the log folder

This is about how to clean apach2/nginx log

Step 2

Navigate to the service folder whichever you are using apache2 or Nginx

  • cd /var/log/apach2
  • sudo rm -rf *.gz

Script to delete the log file after a given time

This script will automatically delete the log files from the server at the specified time so that you need not do it manually every time you find that space is getting full.

Step 1

Create a scripts directory

sudo mkdir /scripts

Step 2

Navigate to the scripts directory

cd /scripts

Step 3

Create a new file inside the folder

sudo touch

Step 4

Edit the log-clean file

sudo vim

Copy the below content in the log-clean file

sudo rm -rf /var/log/*.gz
sudo rm -rf /var/log/apache2/*.gz

Step 5

Now give the file executable permission

sudo chmod a+x

Create a cronjob for the file to be executed by its own

Step 6

You have to specify the time a which the log-clean should execute you can get the time via

Step 7

Edit the crontab file to schedule it

sudo crontab -e

Step 8

In our case, we have scheduled it to every day at the start of the day

0 0 * * * sh /scripts/

Save the file with Ctrl+X and press Y


By this, you can automate the log clean process which does free the huge space occupied by the log files and decreases the associated cost.

Talk to AWS Certified Consultant

    Spread Love By Sharing:

    Let’s Talk About Your Needed AWS Infrastructure Management Services

    Have queries about your project idea or concept? Please drop in your project details to discuss with our AWS Global Cloud Infrastructure service specialists and consultants.

    • Swift Hiring and Onboarding
    • Experienced and Trained AWS Team
    • Quality Consulting and Programming
    Let’s Connect and Discuss Your Project