本人碰到模糊目录移除,小记一下
1:准确目录情况 2:模糊目录情况
先来介绍准备目录情况
本人在网上找到的demo, 目录结构(在/root/zlogs)
脚本文件b.sh
#!/bin/bash
#移除模糊目录下的日期文件夹 RemoveDir=/root/zlogs
dt=`date +%Y-%m-%d -d "7 day ago"` for subdir in `ls $RemoveDir`;
do
if [ "${subdir}" \< "${dt}" ];
then
rm -rf $RemoveDir/$subdir >/dev/null
echo "The directory(date) $RemoveDir/$subdir has been removed."
fi
done
模糊目录, 结构/root/tlogs/*/yyyy-MM-dd
脚本文件a.sh
#!/bin/bash
#移除模糊目录下的日期文件夹 RemoveDir=/root/tlogs/*
dt=`date +%Y-%m-%d -d "7 day ago"` for subdir in `ls $RemoveDir`;
do
if [ "${subdir}" \< "${dt}" ];
then
#此段脚本判断日期是否合法
if echo $subdir | grep -Eq "[0-9]{4}-[0-9]{2}-[0-9]{2}" && date -d $subdir +%Y%m%d > /dev/null 2>&1
then :
else
echo "The directory(device) $subdir don't remove."
#echo "输入的日期格式不正确,应为yyyy-mm-dd";
continue;
fi
#删除目录 有错误不显示
rm -rf $RemoveDir/$subdir >/dev/null
echo "The directory(date) $RemoveDir/$subdir has been removed."
fi
done