19,612
社区成员
发帖
与我相关
我的任务
分享
#!/bin/bash
dir=/tmp/backup
for i in $dir/*;do
basename=`basename $i`
year_str=$(expr substr $basename 9 4)
day_str=$(expr substr $basename 15 2)
if [ "$year_str" -lt 2018 ];then
[ "$day_str" -eq 1 -o "$day_str" -eq 10 -o "$day_str" -eq 20 -o "$day_str" -eq 30 ] || echo "you will remove these files: $i"
# [ "$day_str" -eq 1 -o "$day_str" -eq 10 -o "$day_str" -eq 20 -o "$day_str" -eq 30 ] || rm -rf $i
fi
done
测试下,如果echo的输出是你想要删除的文件,把上面的注释行取消注释
#!/bin/bash
dir=/web/backup
for i in $dir/*;do
year_str=$(echo ${i:20:4})
day_str=$(echo ${i:26:2})
if [ "$year_str" -lt 2018 ];then
[ "$day_str" -eq 1 -o "$day_str" -eq 10 -o "$day_str" -eq 20 -o "$day_str" -eq 30 ] || echo "you will remove these files: $i"
[ "$day_str" -eq 1 -o "$day_str" -eq 10 -o "$day_str" -eq 20 -o "$day_str" -eq 30 ] || rm -rf $i
fi
done