linux运行的系统中长时间运行可能会产生大量的日志文件或其它文件,如果不定时删除,会影响系统的某些功能,下面是通过crontab定时执行删除脚本来实现:
cat delbak.sh
#!/bin/sh
location="/root/sqlbak/"
find $location -mtime +30 -type f |xargs rm -f
find $1 -mtime +$2 -type f -print >>delte.txt
find $1 -mtime +$2 -type f |xargs rm -f
其中:脚本传入得参数$1 $2 传参
ps:
location 是设置查找的目录
–mtime +30 是设置时间为30天前
-type f 这周查找的类型为文件
然后加入crontab定时来删除
crontab -l
crontab配置文件如下,启动crontab后,会定时调用 /root/soft_shell/delbak.sh脚本执行相关操作
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 {} \;