linux定时删除文件

  1. 创建shellauto-del-files.sh
touch /home/suzhuichang/auto-del-files.sh

chmod +x auto-del-files.sh

新建一个可执行文件auto-del-files.sh,并分配可运行权限

2.编辑shell脚本

vi auto-del-files.sh

编辑auto-del-files.sh文件如下:

#!/bin/sh
find /home/suzhuichang/importZipDes -mtime +10 -name "*" -exec rm -rf {} \;

ok,保存退出(:wq)

3.计划任务

crontab -e

将auto-del-files.sh执行脚本加入到系统计划任务,到点自动执行.输入:

10 0 * * * /home/suzhuichang/auto-del-files.sh

这里的设置是每天凌晨0点10分执行auto-del-files.sh文件进行数据清理任务了。

4.错误 -bash: xxx: /bin/sh^M: bad interpreter: No such file or directory

使用notepad++打开sh文件,将文件转为Unix(LF) UTF-8格式保存,上传即可。

参考资料:

http://c.biancheng.net/view/1092.html

https://tool.lu/crontab/

https://www.cnblogs.com/panie2015/p/8523377.html