File logs prior to Linux deletion

Source: Internet
Author: User
Tags log log time and date

Linux under the daily production log log file, if not regularly deleted, sooner or later squeeze the hard disk, if manually deleted, several times can, not a permanent. These things to the Linux system can be solved.

Test environment:

System: CentOS 6.5 x64

Test path, test file name, test time (2017-06-05)

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/97/ED/wKioL1k1FIyh56LhAAAfbB6qlD8846.png "title=" Delete old log_.png "alt=" Wkiol1k1fiyh56lhaaafbb6qld8846.png "/>


Execute command:

# find/root/text-mtime +3-name "log.*"-exec rm-f {} \;

Command explanation:

Find Lookup command

/root/text Find a path

-mtime File Last modified time

+3 3 days ago (or interpreted as starting from the current date of the command execution, the day does not count, keep 3 files)

-name query by query file name

"Log.*" finds files after the beginning of the log for any length character (I don't know if I can find the regular expression tutorial)

-exec fixed notation

Rm-f Eligible Execution Delete does not prompt

{} fixed notation, notice space around

\; Fixed notation, note that there are spaces around


-mtime explains that there are only three time attributes for files in Linux:

Atime (Access time) file last accessed.

Mtime (Modified time) file was last modified.

The CTime (change time) file was last modified with permission times. (e.g. performed chmod)


Using the stat command to see the file details, find finds the file's modified time as a parameter.

# stat Log.2017-06-01

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M00/97/EE/wKioL1k1GASDQn8lAAAfyhFCzgY386.png-wh_500x0-wm_ 3-wmp_4-s_1161877129.png "title=" delete old Log_1.png "alt=" Wkiol1k1gasdqn8laaafyhfczgy386.png-wh_50 "/>

It has been noted that the file name is 06-01, but the display time is 06-05, what's going on. Because you can use the "touch-d" command to modify the modified, the file is now modified back to the original date 06-01.

# touch-d "21:55 06/01/2017" log.2017-06-01 or # touch-d 06/01/2017 log.2017-06-01

The first of the above command is the time and date are modified, the following is only modified date.

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M02/97/EE/wKioL1k1GrHRf-1LAAAe4OH-6jQ805.png "title=" Delete old log_2.png "width=" "height=" 137 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:600PX;HEIGHT:137PX; "alt=" Wkiol1k1grhrf-1laaae4oh-6jq805.png "/>

Alternatively, you can modify the time or the month individually, as follows

To use AM, PM or 24-hour format, dates can be used in other formats such as 6 May 2000.

touch-d "6:03pm" file touch-d "05/06/2000" file touch-d "6:03pm 05/06/2000" file


touch-d "2017-05-31 08:10:30" test.doc Modify the file modification time and access time Touch-m-D "2017-05-31 08:10:30" Test.doc only modify the file modification time Touch-a -D "2017-05-31 08:10:30" Test.doc only modify the access time of the file


Above the Find command, we have written the automatic execution of our join system. Command:

# CRONTAB-E

After opening and Operation VI editing software (because crontab default is to use VI to edit)

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/97/ED/wKiom1k1Ho_BR7PVAAAHjNhJ-eI754.png "title=" Delete old log_3.png "alt=" Wkiom1k1ho_br7pvaaahjnhj-ei754.png "/>

# crontab-l

Lists the contents and defines 2 o'clock in the morning execution.

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/97/EF/wKioL1k1Hs7xBxC5AAAHR96QtiA138.png "title=" Delete old log_4.png "alt=" Wkiol1k1hs7xbxc5aaahr96qtia138.png "/>



Reference article:

http://blog.csdn.net/maqingli20/article/details/7195408

Https://zhidao.baidu.com/question/342860050.html

Http://www.linuxidc.com/Linux/2015-11/124813.htm

Http://www.2cto.com/os/201203/122934.html




This article is from the "Enlightened Grocery store" blog, please be sure to keep this source http://wutou.blog.51cto.com/615096/1932446

File logs prior to Linux deletion

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.