Linux shell:定时删除指定时间之前的文件

要求删除一个月之前的备份文件,免得把硬盘给整爆了

cat delbak.sh
/#!/bin/sh
location="/root/sqlbak/"
find $location -mtime +30 -type f |xargs rm -f

location 是设置查找的目录
--mtime +30 是设置时间为30天前
-type f 这周查找的类型为文件

然后加入crontab定时来删除:

crontab -l
10 4 1 * * /bin/sh /root/soft_shell/delbak.sh

设定为每个月1号晚上4点10分执行脚本.当然你也可以根据你自己的需求去整.

相同的删除方法:

find /root/sqlbak -mtime +30 -type f -name *.gz -exec rm -f {} ;

你可能感兴趣的:(Linux shell:定时删除指定时间之前的文件)