Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

How to Schedule Delete a Log File in Linux

October 23, 2022
in Blog, Linux
0
ADVERTISEMENT

Table of Contents

In this article I will show very basic shell Script to Delete Log Files in Linux and schedule logs deletion using a cron job.

Linux Log Files

The Linux operating system and running applications constantly generate various types of messages that are logged in various log files.

Most of the log files are contained in the /var/log directory.

[root@kms log]# du -sh /var/log
30GB    /var/log

Script to delete a log file in Linux

1. Create a directory where we will store our script file.

cd /usr
mkdir script

2. Create new shell script file and make it executable:

cd script
touch delete_logs.sh
chmod +x delete_logs.sh

3. In this case we need delete this log /var/log/vlmcsd.log

4. Edit the delete_logs.sh file with your favorite text editor and paste text below into that file:

# Linux script to delete Log files weekly
#!/bin/sh

sudo rm -rf /var/log/vlmcsd.log
Bg2182

Then save and close.

4. Now we need create cronjob for execute our delete_logs.sh script.

This cron job will executed once a week at 00:00 on Sunday .

To edit crontab file, let’s run the following command:

sudo crontab -e

Add the following line into the crontab file to delete the log file every week automatically.

0 0 * * 0 /bin/sh /usr/script/delete_logs.sh
Bg2183

Save and close crontab. Now cronjob will execute delete_logs.sh script every week.

More schedules option:

  • every 5th minute: */5 * * * *
  • every 15th minute: */15 * * * *
  • every 60th minute: 0 * * * *

Each line of a crontab file represents a job, and looks like this:

# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday;
# │ │ │ │ │                                   7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# * * * * * <command to execute>

You can use quick and simple editor for editing cron schedule expressions.

Delete multiple log files in Linux

In some cases, you want to delete all vls_proxy1.log, vls_proxy2.log, vls_proxy3.log…, log files, so you have selected only these files: *proxy*.log.

Edit the delete_logs.sh with the asterisk in the log files name.

# Linux script to delete Log files weekly
#!/bin/sh

sudo rm -rf /var/log/*proxy*.log

Conclusion

Hope this post help you learned how to write basic Linux shell script for delete Log files. And make it executable.

5/5 - (1 vote)
Previous Post

How to Open Firewall Ports in Windows using PowerShell

Next Post

How to Delete Files Older Than X Days Automatically in Linux

Related Posts

Running Hyper-V and VMware Workstation on The Same Machine

August 15, 2024

How to Uninstall All Autodesk Products At Once Silently

July 29, 2024
Ftr5

How to Uninstall the Autodesk Genuine Service on Windows

July 29, 2024
Ftr19

How to Fix Windows Cannot Read the ProductKey From the Unattend Answer File in VirtualBox

July 26, 2024
Ftr25

How to Update Windows Terminal in Windows 10/11

July 26, 2024

How to Disable The Beep Sound in WSL Terminal on Windows

July 26, 2024

Leave a Reply Cancel reply

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

Recent Posts

  • How To Turn On uBlock Origin Extension in Chrome (2025)
  • Images Hidden Due To Mature Content Settings In CivitAI
  • Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2025 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory