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: 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: 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 3. Execute the my_script 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:
|
<<: MySQL 5.7.17 installation and configuration method graphic tutorial under win7
>>: Two ways to declare private variables in JavaScript
Preface Because this is a distributed file system...
Table of contents Use of Vue mixin Data access in...
1. Project Structure 2.CallTomcat.java package co...
This article shares the installation and configur...
When we write code, we often need to know the dif...
1. Foreign key setting method 1. In MySQL, in ord...
background nginx-kafka-module is a plug-in for ng...
I found a lot of websites that use drop-down or sl...
Add rules to the el-form form: Define rules in da...
Pseudo-arrays and arrays In JavaScript, except fo...
1 Introduction Kong is not a simple product. The ...
I wonder if you are like me, a programmer who arr...
Table of contents introduction 1. Code to start t...
Related Documents Part of this article is referen...
This article records the installation and configu...