概述
多看shell脚本实例自然就会有shell脚本的编写思路了。下面分享一个shell脚本实例,可以拿去无需直接使用。
作用
清理超过一定时间的日志文件/文件夹
批量清理多路径的过期文件夹
服务器运行的时间长了,就会产生越来越多的日志文件,垃圾文件,因此需要定期清理。
脚本内容如下:
#!/bin/sh#需要删除的日志的绝对路径,每个目录一行paths=(/opt/vplus/pqs/pqs_shde1/log//opt/vplus/pqs/pqs_shde2/log//opt/vplus/pqs/pqs_shde3/log//opt/vplus/pqs/pqs_duijie1/log//opt/vplus/pqs/pqs_duijie2/log/)#删除多久以前的日志目录day=50if [ $day -lt 30 ];then echo It is not recommended to delete logs within $day days exit 0fifor path in ${paths[@]};do if [ -d $path ];then echo $path is exist. logs=`find $path -type d -mtime +$day` array=($logs)files=${#array[@]}if [ $files -gt 0 ];thenecho "These folders will be deleted."for log in ${logs[@]}; do echo [`date +"%F %T"`] delete $log. rm $log -rf doneelseecho These are no log files more than $day day in $path.;echo ""fi else echo $path is not exist.;echo "" fidone
添加执行权限
[root@pqs ~]# chmod +x deletelog.sh
运行效果1
无需要删除文件
[root@pqs ~]# ./deletelog.sh /opt/vplus/pqs/pqs_shde1/log/ is exist.These are no log files more than 150 day in /opt/vplus/pqs/pqs_shde1/log/./opt/vplus/pqs/pqs_shde2/log/ is exist.These are no log files more than 150 day in /opt/vplus/pqs/pqs_shde2/log/./opt/vplus/pqs/pqs_shde3/log/ is exist.These are no log files more than 150 day in /opt/vplus/pqs/pqs_shde3/log/./opt/vplus/pqs/pqs_duijie1/log/ is not exist./opt/vplus/pqs/pqs_duijie2/log/ is not exist.
运行效果2
有可删除的文件
[root@pqs ~]# ./deletelog.sh /opt/vplus/pqs/pqs_shde1/log/ is exist.These are no log files more than 50 day in /opt/vplus/pqs/pqs_shde1/log/./opt/vplus/pqs/pqs_shde2/log/ is exist.These folders will be deleted.[2020-11-30 10:50:46] delete /opt/vplus/pqs/pqs_shde2/log/20201005.[2020-11-30 10:50:46] delete /opt/vplus/pqs/pqs_shde2/log/20201004.[2020-11-30 10:50:46] delete /opt/vplus/pqs/pqs_shde2/log/20201001.[2020-11-30 10:50:46] delete /opt/vplus/pqs/pqs_shde2/log/20201002.[2020-11-30 10:50:46] delete /opt/vplus/pqs/pqs_shde2/log/20201003./opt/vplus/pqs/pqs_shde3/log/ is exist.These folders will be deleted.[2020-11-30 10:50:46] delete /opt/vplus/pqs/pqs_shde3/log/20201001.[2020-11-30 10:50:47] delete /opt/vplus/pqs/pqs_shde3/log/20200927.[2020-11-30 10:50:47] delete /opt/vplus/pqs/pqs_shde3/log/20200928.[2020-11-30 10:50:47] delete /opt/vplus/pqs/pqs_shde3/log/20200930.[2020-11-30 10:50:48] delete /opt/vplus/pqs/pqs_shde3/log/20201002.[2020-11-30 10:50:48] delete /opt/vplus/pqs/pqs_shde3/log/20200929.[2020-11-30 10:50:48] delete /opt/vplus/pqs/pqs_shde3/log/20201004.[2020-11-30 10:50:48] delete /opt/vplus/pqs/pqs_shde3/log/20201003./opt/vplus/pqs/pqs_duijie1/log/ is not exist./opt/vplus/pqs/pqs_duijie2/log/ is not exist.
扩展
1、以上脚本是把整个文件夹删除,如不需要删除文件夹,只删除文件,则把-type d改为-type f
-type d 文件类型为文件夹
-type f 文件类型为文件
logs=`find $path -type d -mtime +$day`修改为logs=`find $path -type f -mtime +$day`
2、如只需要删除当前的文件夹内的日志文件(不含子文件夹)则
-maxdepth 1 最大层级为1,即当前目录
logs=`find $path -type d -mtime +$day`修改为logs=`find $path -maxdepth 1 -type f -mtime +$day`
后面会分享更多Shell方面的内容,感兴趣的朋友可以关注下~
如果你觉得这篇文章对你有帮助, 请小小打赏下.
![2ff89942-5433-eb11-8da9-e4434bdf6706.png](http://p04.5ceimg.com/content/2ff89942-5433-eb11-8da9-e4434bdf6706.png)