Linux Application Summary (1): Automatically delete logs n days ago
Source: Internet
Author: User
Linux Application Summary (1): Automatically delete logs n days ago linux is a system that can automatically generate files, logs, emails, backups, and so on. Although hard disks are cheap now, we can have a lot of hard disk space to waste these files, so that the system can regularly clean up unnecessary files... linux Application Summary (1): Automatically delete logs n days ago linux is a system that can automatically generate files, logs, emails, backups, and so on. Although hard disks are cheap now, we can have a lot of hard disk space to waste these files, so that the system can clean up unnecessary files at regular intervals. You don't have to worry about whether you need to clear logs every day, and you don't need to receive alarm text messages due to insufficient disk space every day. if you want to have a good rest, let's hand over this to the machine for scheduled execution. 1. command to delete a file: find the corresponding directory-mtime + days-name "file name"-exec rm-rf {}\; instance command: find/opt/soft/log/-mtime + 30-name "*. log "-exec rm-rf {}\; note: add ". log files. The specific parameters are described as follows: find: linux search command, which allows you to find files with specified conditions;/opt/soft/log/: any directory to be cleaned;-mtime: standard statement syntax; + 30: search for files 30 days ago. here, numbers are used to represent the number of days ;"*. log ": the data type to be searched ,"*. jpg "indicates to search for all files with the jpg extension, and" * "indicates to search for all files, which can be used flexibly.-exec: fixed writing; rm-rf: force delete files, including Directory; {}\;: fixed writing method, a pair of braces + spaces + \ +; 2. scheduled Tasks: if it is too difficult to manually execute the statement each time, you can write the small statement to an executable shell script file and then set cron to schedule the execution, then the system can automatically clean up related files. 2.1 create shell: touch/opt/soft/bin/auto-del-30-days-ago-log.shchmod + x auto-del-30-days-ago-log.sh create a new executable file auto-del-30-days-ago-log.sh and assign runnable permissions 2.2 edit shell script: vi auto-del-30-days-ago-log.sh edit auto-del-30-days-ago-log.sh file as follows: #! /Bin/shfind/opt/soft/log/-mtime + 30-name "*. log"-exec rm-rf {}\; OK, save and exit (: wq ). 2.3 scheduled task: # crontab-e add the auto-del-30-days-ago-log.sh execution script to the system scheduled task, point-to-point automatic execution input: 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 00:10 every day for data cleanup tasks. After completing the preceding three steps, you no longer need to remember whether the hard disk space is full every day. you need to clear the log file and no longer receive alarms due to insufficient disk space on the server, rest assured, read books and drink coffee!
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.