Shell script for automatically deleting files on the server

Source: Internet
Author: User
The shell script used by the server to automatically delete files is added to the system scheduler to automatically clean up junk files, such as unwanted log files. Support automatic cleanup when the matching path matches the file name that has not been accessed # used to clean up the file script in various systems, filepathreg_filenamefileatim... the shell script used by the server to automatically delete files is added to the system scheduler to automatically clean up junk files, such as unwanted log files. Support matching path matching file name for how long there is no access to automatic cleaning 01 # for the system to clean up the file script, filepath reg_filename fileatime02 # author Foyon0806@gmail.com03 # date 5# site www. jbxue. com05 #! /Bin/sh06if [$ #-eq 0]; then07 echo "Usage: sh auto_clear_file.sh clear_filepath clear_regfilename filecreatetime" 08 echo "eg: sh auto_clear_file.sh/tmp/log/user_log-7day "09 exit10fi11filepath = $112 regfilename = $213 14 15if ["-$3 "="-"]; then16 filectime = 'date-d-7day' + % s' 17else18 filectime = 'date-d $ 3' + % s' '19fi20log = 'ls $ filepath | grep $ regfilename '21echo $ log22for file in $ {log} 23do24 echo $ file25 fileatime = 'stat-c % X $ {filepath }$ {file} '26 if [$ {fileatime}-lt $ {filectime}]; then27 opt = 'rm-f $ {filepath }$ {file} '28 echo $ opt29 fi 30 done
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.