Linux automatically deletes logs and example commands from n days ago

Linux automatically deletes logs and example commands from n days ago

1. Delete file command:

find the corresponding directory -mtime + number of days -name "file name" -exec rm -rf {} \;

Example command:

find /opt/soft/log/ -mtime +30 -name "*.log" -exec rm -rf {} \;

illustrate:

Delete all files with ".log" in the /opt/soft/log/ directory that are older than 30 days. The specific parameters are described as follows:

find: Linux search command, users search for files with specified conditions;

/opt/soft/log/: any directory you want to clean;

-mtime: standard statement writing method;

+30: Search for files 30 days ago, where numbers represent the number of days;

"*.log": the type of data you want to find, "*.jpg" means to find all files with the extension jpg, "*" means to find all files, this can be used flexibly, and you can draw inferences from one example;

-exec: fixed writing;

rm -rf: forcefully delete files, including directories;

{} \; : Fixed writing method, a pair of curly brackets + space + \+;

2. Planned tasks:

If you think it is too troublesome to execute the statement manually every time, you can write this small statement into an executable shell script file, and then set up cron scheduling execution, so that the system can automatically clean up related files.

2.1 Create a shell:

touch /opt/soft/bin/auto-del-30-days-ago-log.sh
chmod +x auto-del-30-days-ago-log.sh

Create a new executable file auto-del-30-days-ago-log.sh and assign it executable permissions

2.2 Edit the shell script:

vi auto-del-30-days-ago-log.sh

Edit the auto-del-30-days-ago-log.sh file as follows:

#!/bin/sh
find /opt/soft/log/ -mtime +30 -name "*.log" -exec rm -rf {} \;

OK, save and exit (:wq).

2.3 Planned tasks:

#crontab -e

Add the auto-del-30-days-ago-log.sh execution script to the system scheduled tasks and execute it automatically at a certain time

enter:

10 0 * * * /opt/soft/log/auto-del-7-days-ago-log.sh >/dev/null 2>&1

The setting here is to execute the auto-del-7-days-ago-log.sh file at 0:10 am every day to perform data cleaning tasks.

After completing the above three steps, you will no longer have to worry about whether the hard disk space is full and it is time to clean up the log files. You will no longer receive alarm messages about insufficient hard disk space on the server. You can go read a book and drink coffee with peace of mind!

Each time you add a task, be sure to restart the crond service, otherwise it will not take effect

The code is as follows:

service crond restart

Creating Your First Shell Script

1. Write a script

a) Create a file using the touch command: touch my_script

b) Open the my_script file with the vim editor: vi my_script

c) Edit the my_script file with the vim editor. The content is as follows:

#!/bin/bash tells the shell what program to use to interpret the script #My first script
ls -l .*

2. Allow Shell to execute it

chmod 755 my_script

3. Execute the my_script script

./my_script

Summarize

The above is what I introduced to you about Linux automatic deletion of logs n days ago. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • Linux uses scheduled tasks to clean up logs 45 days ago every week
  • Scheduled cutting of Tomcat logs in Linux and deleting log records older than a specified number of days
  • How to write a script to cut nginx logs every day under Linux system
  • Detailed explanation of the process of log timing polling under Linux
  • Scheduled cutting of Mongodb database logs in Linux and deleting log records older than a specified number of days
  • Code to delete logs 7 days ago under Linux (php+shell)
  • How to automatically delete archive log files under Linux
  • A simple method to implement Linux timed log deletion

<<:  MySQL 5.7.17 installation and configuration method graphic tutorial under win7

>>:  Two ways to declare private variables in JavaScript

Recommend

Practical experience of implementing nginx to forward requests based on URL

Preface Because this is a distributed file system...

Detailed explanation of the use of Vue mixin

Table of contents Use of Vue mixin Data access in...

How to start/stop Tomcat server in Java

1. Project Structure 2.CallTomcat.java package co...

MySQL 8.0.15 winx64 installation and configuration method graphic tutorial

This article shares the installation and configur...

Summary of 9 excellent code comparison tools recommended under Linux

When we write code, we often need to know the dif...

MySQL foreign key setting method example

1. Foreign key setting method 1. In MySQL, in ord...

Example of integrating Kafka with Nginx

background nginx-kafka-module is a plug-in for ng...

Drop-down menu and sliding menu design examples

I found a lot of websites that use drop-down or sl...

Vue implements form data validation example code

Add rules to the el-form form: Define rules in da...

The use and difference between JavaScript pseudo-array and array

Pseudo-arrays and arrays In JavaScript, except fo...

Detailed tutorial on installing and using Kong API Gateway with Docker

1 Introduction Kong is not a simple product. The ...

How to Delete Junk Files in Linux Elegantly

I wonder if you are like me, a programmer who arr...

MySQL 8.0.15 installation and configuration method graphic tutorial

This article records the installation and configu...