Linux application: automatically delete logs n days ago and logs n days ago

Source: Internet
Author: User

Linux application: automatically delete logs n days ago and logs n days ago

Linux is a system that automatically generates files, such as logs, emails, and backups. 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. File Deletion command:
Find directory-mtime + days-name "file name"-exec rm-rf {}\;
Instance command:
Find/opt/soft/log/-mtime + 30-name "*. log"-exec rm-rf {}\;
Note:
Delete all files with ". log" 30 days ago in the/opt/soft/log/directory. The parameters are described as follows:
Find: linux SEARCH command. You can search for files with specified conditions;
/Opt/soft/log/: any directory to be cleaned;
-Mtime: Standard statement writing;
+ 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. This can be used flexibly;
-Exec: Fixed statement;
Rm-rf: Force delete files, including directories;
{}\;: Fixed syntax, a pair of braces + spaces ++;
2. scheduled tasks:
If it is too troublesome 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, so that the system can automatically clean up the relevant 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 an executable auto-del-30-days-ago-log.sh and assign runnable 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/shfind /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 task, to point for 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!

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.