歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> SHELL編程 >> 服務器自動刪除文件的shell腳本

服務器自動刪除文件的shell腳本

日期:2017/3/1 15:17:14   编辑:SHELL編程
服務器自動刪除文件的shell腳本 加入系統 計劃程序中,自動清理垃圾文件,如不需要的日志文件。 支持匹配路徑 匹配文件名 多久沒有訪問的自動清理 01 #用於各系統清理文件腳本,filepath reg_filename fileatime 02 #author [email protected] 03 #date 2013-8-22 14:51:52 04 #site www.jbxue.com 05 #!/bin/sh 06 if [ $# -eq 0 ];then 07 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 exit 10 fi 11 filepath=$1 12 regfilename=$2 13 14 15 if [ "-$3" = "-" ];then 16 filectime=`date -d -7day '+ %s'` 17 else 18 filectime=`date -d $3 '+ %s'` 19 fi 20 log=`ls $filepath | grep $regfilename` 21 echo $log 22 for file in ${log} 23 do 24 echo $file 25 fileatime=`stat -c %X ${filepath}${file}` 26 if [ ${fileatime} -lt ${filectime} ]; then 27 opt=`rm -f ${filepath}${file}` 28 echo $opt 29 fi 30 done
Copyright © Linux教程網 All Rights Reserved