Linux automatically delete n days ago Log

Source: Internet
Author: User

1. Delete file command:

Find corresponding directory-mtime + days-name "file name"-exec rm-rf {} \;

Instance command:

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

Description

Remove all files with ". Log" 30 days ago in the/opt/soft/log/directory . The specific parameters are described as follows:

Find:linux the search command, the user finds the file of the specified condition ;

/opt/Soft/log/: Any directory that you want to clean up ;

-mtime: standard sentence notation ;

+30: Find the file 30 days ago, here the number of days to represent ;

"*. log": The type of data you want to find," *.jpg "means to find all files with the extension jpg," * "means to find all the files, this can be used flexibly, extrapolate ;

-exec: fixed notation ;

RM-RF: Forced deletion of files, including directories ;

{} \; : fixed notation, pair of curly braces + space +\+;

2. Scheduled Tasks:

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

2.1 Creating the 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 runnable permissions

2.2 Edit 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 exit (: Wq).

2.3 Scheduled Tasks:

#crontab-E

Add the auto-del-30-days-ago-log.sh execution script to the system scheduled task, to the point of automatic execution

Input:

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 for the Data Cleanup task 0:10 A.M. every day .

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.