[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]#
发布了89 篇原创文章 · 获赞 1 · 访问量 4823

猜你喜欢

转载自blog.csdn.net/wy_hhxx/article/details/103298287