Aug
14
2024
自动删除指定目录(多个)下的最后修改时间超过指定天数的文件的bash脚本
作者:
绝缘体.. 发布:
2024-08-14 09:31 分类:
未分类 阅读:
抢沙发
自动删除指定目录(多个)下的最后修改时间超过指定天数(默认365天,一年)的文件的 bash 脚本。
#!/bin/bash
#自动删除指定目录(多个)下的最后修改时间超过指定天数(默认365天,一年)的文件
# 定义包含目录路径的数组
directories=("/home/www/www.phpernote.com/logs" "/path/to/dir2")
# 定义天数
days=365
# 定义一个空数组来存储文件
files_to_delete=()
# 遍历数组中的每个目录
for dir in "${directories[@]}"; do
# 检查目录是否存在
if [ -d "$dir" ]; then
while IFS= read -r -d '' file; do
files_to_delete+=("$file")
done < <(find "$dir" -type f -mtime +${days} -print0)
# 遍历数组并删除文件
for file in "${files_to_delete[@]}"; do
#rm -f "$file"
echo "文件 $file 的最后修改时间已超过一年"
done
echo "目录 $dir 中最后修改时间超过 $days 天的文件已经删除完毕"
# 清空数组,为下一个目录准备
files_to_delete=()
else
echo "目录 $dir 不存在!"
fi
done
微信扫一扫,打赏作者吧~