用shell写一个脚本,定时删除/var/log/kudu目录下的日志,要求只删除名字包含info的.log日志,只保留最新三天内的日志
时间: 2023-02-28 18:46:52 浏览: 58
以下是一个 shell 脚本的示例:
```
#!/bin/bash
# 获取当前时间
now=$(date +%s)
# 计算三天前的时间戳
three_days_ago=$((now - 3 * 24 * 3600))
# 进入日志目录
cd /var/log/kudu
# 遍历所有.log日志
for log in *.log; do
# 判断文件名是否包含"info"
if [[ "$log" == *"info"* ]]; then
# 获取文件的修改时间
mtime=$(stat -c %Y "$log")
# 判断文件的修改时间是否早于三天前
if [[ "$mtime" -lt "$three_days_ago" ]]; then
# 删除文件
rm "$log"
fi
fi
done
```
请注意,上面的代码仅供参考,在您的实际环境中可能需要做一些修改。此外,请谨慎运行任何删除文件的脚本,以免误删除重要文件。