[Linux] 刪除指定路徑下指定時間範圍之外的文件

可以使用如下腳本clearFiles.sh結合crontab實現 刪除指定路徑下指定時間範圍之外的文件。

#!/bin/bash

#config time range to 1440min=24h
KEEP_TIME=1440

pathlist=`cat /opt/clearFileFolder.txt`
for FILE_PATH in $pathlist
do
  find ${FILE_PATH} -type f -mmin +${KEEP_TIME} | xargs rm -f 2>/dev/null
done

說明:
將需要檢查的路徑寫在clearFileFolder.txt中,一行一個路徑
KEEP_TIME後配置保留文件的時間範圍(分鐘),上例中1440min即24h
crontab中加入行 00 02 * * * /var/clearFiles.sh (每天凌晨2點執行)

關於find命令按時間查找文件

-atime/-mtime/-ctime  (+/-)N

          +N

<-——————>

          N

<-——————>

          -N

<-——————>

(N+1)*24h 之前 (N+1)*24h ~ N*24h之間 N*24h 之內

 

 

 

[例] 按照修改時間-mtime和-mmin查找文件

[root@xxx test_retention]# date
Thu Jan 16 13:54:13 CST 2020
[root@xxx test_retention]# touch -d "2020-01-16 13:00:00" 0116-1300.txt
[root@xxx test_retention]# touch -d "2020-01-16 09:00:00" 0116-0900.txt
[root@xxx test_retention]# touch -d "2020-01-15 09:00:00" 0115-0900.txt
[root@xxx test_retention]# touch -d "2020-01-15 13:00:00" 0115-1300.txt
[root@xxx test_retention]# touch -d "2020-01-14 13:00:00" 0114-1300.txt
[root@xxx test_retention]# touch -d "2020-01-14 09:00:00" 0114-0900.txt
[root@xxx test_retention]#
[root@xxx test_retention]# ls -l
total 0
-rw-r--r--. 1 root root 0 Jan 14 09:00 0114-0900.txt
-rw-r--r--. 1 root root 0 Jan 14 13:00 0114-1300.txt
-rw-r--r--. 1 root root 0 Jan 15 09:00 0115-0900.txt
-rw-r--r--. 1 root root 0 Jan 15 13:00 0115-1300.txt
-rw-r--r--. 1 root root 0 Jan 16 09:00 0116-0900.txt
-rw-r--r--. 1 root root 0 Jan 16 13:00 0116-1300.txt
[root@xxx test_retention]#
[root@xxx test_retention]# find ./ -type f -mtime -1
./0116-1300.txt
./0116-0900.txt
[root@xxx test_retention]# find ./ -type f -mtime 1
./0115-0900.txt
./0115-1300.txt
[root@xxx test_retention]# find ./ -type f -mtime +1
./0114-1300.txt
./0114-0900.txt
[root@xxx test_retention]# 
[root@xxx test_retention]# rm -rf ./*
[root@xxx test_retention]#
[root@xxx test_retention]# touch -d "2020-01-15 13:55:00" 0115-1355.txt
[root@xxx test_retention]# touch -d "2020-01-15 14:00:00" 0115-1400.txt
[root@xxx test_retention]# touch -d "2020-01-15 14:05:00" 0115-1405.txt
[root@xxx test_retention]# touch -d "2020-01-15 14:10:00" 0115-1410.txt
[root@xxx test_retention]# touch -d "2020-01-14 14:00:00" 0114-1400.txt
[root@xxx test_retention]# touch -d "2020-01-14 14:05:00" 0114-1405.txt
[root@xxx test_retention]# touch -d "2020-01-14 14:10:00" 0114-1410.txt
[root@xxx test_retention]#
[root@xxx test_retention]# rm -rf 0115-1355.txt
[root@xxx test_retention]# ls -l
total 0
-rw-r--r--. 1 root root 0 Jan 14 14:00 0114-1400.txt
-rw-r--r--. 1 root root 0 Jan 14 14:05 0114-1405.txt
-rw-r--r--. 1 root root 0 Jan 14 14:10 0114-1410.txt
-rw-r--r--. 1 root root 0 Jan 15 14:00 0115-1400.txt
-rw-r--r--. 1 root root 0 Jan 15 14:05 0115-1405.txt
-rw-r--r--. 1 root root 0 Jan 15 14:10 0115-1410.txt
[root@xxx test_retention]# find ./ -type f -mmin -1440
./0115-1410.txt
[root@xxx test_retention]# find ./ -type f -mmin 1440
[root@xxx test_retention]# find ./ -type f -mmin +1440
./0115-1400.txt
./0115-1405.txt
./0114-1400.txt
./0114-1405.txt
./0114-1410.txt
[root@xxx test_retention]# date;find ./ -type f -mmin 1440
Thu Jan 16 14:09:45 CST 2020
./0115-1410.txt
[root@xxx test_retention]# date;find ./ -type f -mmin 1440
Thu Jan 16 14:10:02 CST 2020
[root@xxx test_retention]# date;find ./ -type f -mmin -1440
Thu Jan 16 14:11:34 CST 2020
[root@xxx test_retention]# date;find ./ -type f -mmin +1440
Thu Jan 16 14:11:39 CST 2020
./0115-1400.txt
./0115-1405.txt
./0115-1410.txt
./0114-1400.txt
./0114-1405.txt
./0114-1410.txt
[root@xxx test_retention]#

 

發佈了92 篇原創文章 · 獲贊 2 · 訪問量 5705
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章