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
Practice is the only way to test the truth. This ...
Enter the mysql command: mysql -u+(user name) -p+...
Table of contents 1. unzip command 1.1 Syntax 1.2...
vue+el-upload multiple files dynamic upload, for ...
Find the installation directory of VirtualBox. Th...
This article example shares the specific code of ...
Mac uses Shell (Terminal) SSH to connect to the r...
This article shares the specific code of JavaScri...
Table of contents Introduction Mirror repository ...
Table of contents 1. Global Guard 1. Global front...
Table of contents 1. Self-enumerable properties 2...
Summary: Problem solving records of MYSQL: No mat...
No gaps on both sides, gaps between each column w...
Alibaba Cloud Server cannot connect to FTP FileZi...
XML files should be encoded in utf-8 as much as p...