Linux shell scripts Delete outdated files

Source: Internet
Author: User

Before writing this shell, let's talk about how the stat command is used

First, about time stamp

Each file has 3 of the time (called timestamp timestamps), for these 3 kinds of time, many times easily confused, so here to explain:

  Access Time ( Atime): refers to the time the file is taken, the so-called access, common operations are: using the editor to view the contents of the file, using the Cat command to display the contents of the file, using the CP command to copy the file (i.e. the source file) to other files, or to use grep on this file Sed more less tail head and other commands, usually read without modifying the operation of the file, balanced change the file's access time.

Modify Time (mtime): refers to the change in the content of the file, as long as the contents of the file changes (such as the use of the switch to the output or to attach the way) or the operation of the file, it will change the Modify of the files, usually when we use Ls–l to view the file, Modify time is displayed.

Change Time (CTime): refers to the timing of file attributes or file location changes, such as the use of the CHMOD,CHOWN,MV instruction set to use LN to do a file with a obstinately connected, the file changes will be changed.

Note:

Modify time and change time if you modify the file (use the editor to save or use) >> turn the operation)

The time that the write is updated synchronously, but access is not changed.

If you execute a touch file, the time in 3 changes all

Using Ln–s to make a soft connection to a file will change the file's fetch time

Using Ls–la to view generic files does not change these three times, but if the file is a symbolic link file, it will change the time taken to access it

Ii. about the Stat command

Stat is a way to get additional information about a file and get two kinds of information:

1. File system

2. Documents

Basic usage:

Get File System Properties

Stat–f/dev/sda1

-F: Indicates that the file system (partition) is used instead of the file

Get file attributes

Stat filename

Stat Basic Syntax Format:

Stat–c Format code file

Format code:

For files (not all enumerated)

%A: Using file permission codes to represent permissions

%F: Using octal to represent file permissions

%G: The group name of the owner of the file

%G: Group ID (GID) of the file owner

%i:inode number

%n: File name

%s: File size

%u: File owner name

%u: ID (UID) of the owner of the file

%x: Fetch Time

%y: Modification Time

%z: Property Change Time

For file systems:

%a: Number of blocks that a general user can freely use

%b: Total number of blocks

%c: Total number of file node points

%d: Available file node points

%f: Number of chunks that are desirable

More accessible: http://lovelace.blog.51cto.com/1028430/1212623


Here's the shell script:

#! /bin/bash

Data_path= "/opt/test5" #此处定义数据文件的路径

Expired_time=3 #此处定义文件的过期时间, such as 3 days

function Deletefiles () {

Local currentdate= ' Date +%s ' #获取系统时间, so the time format is seconds

echo "Current date is:" $currentDate

For file in ' Find $1-name ' *.sh ' #此处定义文件格式, avoid accidental deletion, which refers to the parameters of the Deletefiles function below $data_path

Do

Local name= $file

Local modifydate=$ (stat-c%Y $file)

Local existtime=$[$currentDate-$modifyDate] #对比时间, calculate the log existence time, distance from the last modification

existtime=$[$existTime/86400]

If [$existTime-gt $expired _time];then

echo "File:" $name "Modify Date:" $modifyDate + "Exist time:" $existTime + "Delete:yes"

RM-RF $file

Else

echo "File:" $name "Modify Date:" $modifyDate + "Exist time:" $existTime + "

Delete:no "

Fi

Done

}

Deletefiles $data _path


650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/88/A1/wKiom1f9sWKRN_waAABYIi_klOU363.png-wh_500x0-wm_3 -wmp_4-s_2728410817.png "title=" Test20.png "alt=" Wkiom1f9swkrn_waaabyii_klou363.png-wh_50 "/>

The name of my test script is test30.sh, which is also placed under/OPT/TEST5, such as:

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M01/88/A1/wKiom1f9ss2BWa6JAACP496yBq8948.png-wh_500x0-wm_3 -wmp_4-s_2459234050.png "title=" Test21.png "alt=" Wkiom1f9ss2bwa6jaacp496ybq8948.png-wh_50 "/>

Then execute the script, SH test30.sh, and the result is as follows:

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/88/9E/wKioL1f9s4-yY7A-AACUNWxaeMQ275.png-wh_500x0-wm_3 -wmp_4-s_3558223665.png "title=" Test22.png "alt=" Wkiol1f9s4-yy7a-aacunwxaemq275.png-wh_50 "/>


650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/88/A1/wKiom1f9s9eDIBYJAAAS2BK0XPM707.png "title=" Test23.png "alt=" Wkiom1f9s9edibyjaaas2bk0xpm707.png "/>


No problem, we can use the scheduled task to control the shell script; crontab-e

Every month, 15th, 0:0 executes the/opt/test5/test30.sh script:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/89/29/wKiom1gJqiiz9qyFAAATrr6vPv4685.png "title=" Test24.png "alt=" Wkiom1gjqiiz9qyfaaatrr6vpv4685.png "/>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/89/27/wKioL1gJqnDgTXO-AAAc7f--CUQ532.png "title=" Test25.png "alt=" Wkiol1gjqndgtxo-aaac7f--cuq532.png "/>

Detailed crontab can refer to this website: http://www.cnblogs.com/kerrycode/p/3238346.html




This article references the following good articles:

1.http://gaowenlong.blog.51cto.com/451336/1835545

2.http://lovelace.blog.51cto.com/1028430/1212623

3.http://www.cnblogs.com/kerrycode/p/3238346.html




Linux shell scripts Delete outdated files

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.