Shell 定时删除N天前文件 | Eddie'Blog
Shell 定时删除N天前文件

Shell 定时删除N天前文件

eddie 334 2021-07-23

目录

脚本

vim del3dbak.sh

#!/bin/bash

now=`date +%Y-%m-%d-%H:%M:%S`;
echo -e "定时任务启动,释放磁盘空间,删除列表如下:\n"
# find /data/jenkins/backstage-jar-bak/ -name "*.*" -mtime -3
find /data/jenkins/backstage-jar-bak/ -name "api.jar.bak.*" -mtime -3
find /data/jenkins/backstage-jar-bak/ -name "api.jar.bak.*" -mtime -3  -exec rm -rf {} \;

cd /data/jenkins/backstage-jar-bak/
cp /data/jenkins/backstage-jar-bak/api.jar /data/jenkins/backstage-jar-bak/api.jar.bak.${now}
echo -e "定时任务结束,释放磁盘空间\n\n"

赋予权限

chmod 777 del3dbak.sh

定时任务

每周六运行一次脚本,删除三天前文件

crontab -e

* * * * 06 sh /data/jenkins/backstage-jar-bak/del3dbak.sh >> /data/jenkins/backstage-jar-bak/run.log 2>&1