在业务中,给定的磁盘容量小,为了避免爆盘,需要定时清除数据,所以总结了一下设置方法:可以使用find命令
find 路径 -mtime +天数 -type d -name "文件名" -exec rm -rf {} ;
find 路径 -mtime +15-type d -name "*" -exec rm -rf {} ;
find /tmp -mtime +15 -type d -name "*" -exec rm -rf {} \;
/tmp --设置查找的目录;
-mtime +15 --设置修改时间为15天前;
-type d --设置查找的类型为文件;其中f为文件,d则为文件夹
-name "*" --设置文件名称,可以使用通配符;
-exec rm -rf --查找完毕后执行删除操作;
{} \; --固定写法
二、还可以把此命令写入脚本中
1、创建脚本cleandata.sh
vim cleandata.sh
2、写入命令
find /tmp -mtime +15 -type d -name "*" -exec rm -rf {} \;
3、保存文件
按Esc键
:wq #保存
4、配置可执行
chmod u+x ./cleandata.sh
5、配置到crontab
crontab -e
#每天九点十五自动执行
15 9 * * * /home/test/cleandata.sh > /dev/null 2>&1
*号解释:
第一个*号表示时间中的 分钟 取值范围:0-59
第二个*号表示时间中的 小时 取值范围:0-23
第三个*号表示一个月中的第几天,取值范围:1-31
第四个*号表示一年中的第几个月,取值范围:1-12
第五个*号表示一个星期中的第几天,以星期天开始依次的取值为0~7,0、7都表示星期天
以上已经配置完成,亲测有效
---------------------------------------------------------------------------------------------------------------------------------
linux定时删除时间格式文件夹
#!/bin/bash
function deletedir(){
ago_file=$(date -d '30 days ago' +%Y%m%d)
dir_or_file=$1
if [ -d $dir_or_file ]
then
for element_date in `ls $dir_or_file`
do
dir_or_file_date=$dir_or_file"/"$element_date
if [ -d $dir_or_file_date ]
then
date_file=$(date -d $element_date +%Y%m%d)
if [ $date_file -lt $ago_file ]
then
rm -rf $dir_or_file_date
fi
fi
done
fi
}
root_dir="/data0/test"
deletedir $root_dir