Linux automatically deletes backups n days ago

Source: Internet
Author: User
Article Title: linux automatically deletes backups n days ago. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

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, but there are always many requirements ~ I think it is refreshing to make the system clean up unnecessary files on a regular basis ~~

Statement Syntax:

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

Example 1:

Find/usr/local/backups-mtime + 10-name "*. *"-exec rm-rf {}\;

Delete all files with "." In the/usr/local/backups directory 10 days ago

Find: linux SEARCH command. You can find files with specified conditions.

/Usr/local/backups: any directory to be cleaned

-Mtime: Standard statement writing

+ 10: Search for files 10 days ago. Here, the number represents the number of days, and the number plus 30 represents the number of files 30 days ago.

"*. *": The type of data to be searched. "*. jpg" indicates all files with the jpg extension. "*" indicates all files. This can be used flexibly.

-Exec: fixed syntax

Rm-rf: Force delete files, including directories

{}\;: Fixed writing method, a pair of braces + spaces + \+;

If it is too troublesome to manually execute the statement each time, you can write the small statement to an executable file, and then set cron to schedule the execution, so that the system can automatically clear the relevant files.

Example 2:

1. # touch/usr/local/bin/clear

# Chmod 777 clear

Create an executable file clear

2. vi clear

Edit the clear file as follows:

#! /Bin/sh

Find/usr/local/backups-mtime + 10-name "*. *"-exec rm-rf {}\;

OK, save and exit

3. # crontab-e

Add the clear file to the system scheduled task and execute it automatically.

Input:

* 2 ***/usr/local/bin/clear

The setting here is to execute the clear file every morning to clean up the data. You can study cron to develop your own scheduled tasks.

Example:

#! /Bin/sh

Find/usr/local/jboss-4.2.3.GA/server/default/log-mtime + 6-name "server. log. *"-exec rm-rf {}\;

Exit

[Root @ web3 ~] # Crontab-l

* 2 *** sh/root/AutoDelLog. sh

In this way, the script is executed at every night. The script deletes the file six days ago.

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.