[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]#

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值