Linux 定时清理文件脚本

时间:2025-04-10 08:04:28

编写清理脚本,添加到定时任务中:

创建可执行文件
cd /home
touch clear_log.sh
赋予可执行权限
chmod +x clear_log.sh
编写脚本内容
vi clear_log.sh
添加脚本
#!/bin/sh
find /home/zhongli_interface -type f -mtime +3 -name "*.tmp" -exec rm -rf {} \;
/home/zhongli_interface 清理文件的路径
 -type f 清理文件类型为文件,f修改成d 就是文件夹
-mtime +3 清理三天前的文件
 清理文件名为.tmp结尾的文件
 -exec 执行的命令
 {} \; 固定格式
设置定时任务
cd /etc
crontab -e
添加内容(每天凌晨2点执行脚本)
0 2 * * * /home/clear_log.sh
保存退出