2016年8月16日

Linux自動排程刪掉舊檔案

建立自動刪檔Srcipt
vi /root/del-nfsen-oldfile.sh

chmod +x del-nfsen-oldfile.sh
#定期刪除/目錄/內N天前的備份檔 (如:.tar.gz & .zip)
find /目的路徑/ -type f -mtime +幾天數 -exec rm -rf {} \;
(註)
-type t:t 的代表的是檔案的類型 如:d 資料夾 f 檔案 l 鏈結檔
-mtime n :用檔案修改時間(modification)為搜尋條件,n以24小時為週期
-print: 將搜尋結果送到標準輸出到螢幕
-exec command:將搜尋的結果,再導入某一命令。
(PS:搜尋出的結果可用兩個大括號{} 替代,命令後接 \; 表示結束)

將Script 加入到/etc/crontab做排程,便可自動刪掉過期的舊檔目錄與內容
(每月的20號凌晨五5點,自動排程執行script)

0 回應:

Copyright © 2009 New Life in Taipei All rights reserved. Theme by Laptop Geek. | Bloggerized by FalconHive.