Linux-Automatically delete n days ago Log __linux

Source: Internet
Author: User
1. Delete File command:

Find corresponds to directory-mtime + days-name "filename"-exec rm-rf {} \;

Instance command:

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

Description

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

Find:linux Lookup command, the user looks for the specified condition of the file;

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

-mtime: standard sentence formulation;

+30: Find 30 days before the file, where the number of days to represent;

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

-exec: fixed wording;

RM-RF: Force delete file, including directory;

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

2. Planning tasks:

If it is too troublesome to manually execute a statement each time, you can write this small statement into an executable shell script file, and then set up cron scheduling execution, which allows the system to 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 permissions to run

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

2.3 Scheduled Tasks:

#crontab-E

Add the auto-del-30-days-ago-log.sh execution script to the System planning task, which is automatically executed

Input:

0 * * */opt/soft/log/auto-del-7-days-ago-log.sh >/dev/null 2>&1

The setting here is to perform the data Cleanup task on the auto-del-7-days-ago-log.sh file 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.