Linux Application Summary: Automatically delete n days ago Log

Source: Internet
Author: User

Linux It is a system that can produce files automatically, log, mail, backup and so on. Although now hard disk cheap, we can have a lot of hard disk space for these files wasted, so that the system regularly clean up some unnecessary files there is a refreshing thing. Do not have to worry about whether you need to clear the log every day, do not have to receive the hard disk space of the alarm SMS, want to have a good rest, let us put this matter to the machine timed to execute it.

    1. 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

will be /opt/soft/log/ directory under All - days ago with ". Log" files are deleted. The specific parameters are described as follows:

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

/opt/soft/log/ : Any directory for which you want to clean up;

-mtime : standard sentence notation;

+30 : Find - days before the document, where numbers are used to represent the number of days;

"*.log" : The type of data you want to find, "*.jpg" indicates that the lookup extension is jpg all the files, "*" means to find all 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 +\+;  

    1. 2. To Schedule a task:

if it is too much trouble to manually execute the statement every time, you can write this small statement to an executable Shell script file, and then set the Cron schedule execution, that allows the system to automatically clean up the relevant files.

2.1 Create 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 To Schedule a task:

#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 0 hours a day to execute auto-del-7-days-ago-log.sh . file for the Data Cleanup task.

Complete the above three steps, you will never worry about whether the hard disk space is full, the cleanup log file, and will not be the server hard disk space for alarm information, rest assured to read a book Drink coffee!


This article from the "11686974" blog, reproduced please contact the author!

Linux Application Summary: Automatically delete n days ago Log

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.