美文网首页
批量创建或删除特定规律的文件夹或文件

批量创建或删除特定规律的文件夹或文件

作者: 星际探索者 | 来源:发表于2020-03-23 14:22 被阅读0次

    1、create directory

    !/bin/bash

    for i in {1..12}
    do
    for y in {1..31}
    do

        if ((i < 10));then
            if ((y < 10));then
                mkdir -p /opt/logs/2020-0$i-0$y
            else
                mkdir -p /opt/logs/2020-0$i-$y
            fi
        else
            if ((y < 10));then
                mkdir -p /opt/logs/2020-$i-0$y
            else
                mkdir -p /opt/logs/2020-$i-$y
            fi
        fi
    done
    

    done
    which looked like below


    image.png

    2、delete file

    !/bin/bash

    delete file of specifical directory and left only one month file

    get current month

    month=date +"%m"

    remove 0 if month < 10

    if (({month:0:1} == 0));then month={month:1:1}
    fi

    iterator the specifical directory

    for i in ls $1
    do

    rm=${i:5:2}
    if ((${rm:0:1} == 0));then
        rm=${rm:1:1}
    fi
    if ((rm > month));then
        rm -f $1/$i/*
    fi
    

    done

    image.png
    image.png

    相关文章

      网友评论

          本文标题:批量创建或删除特定规律的文件夹或文件

          本文链接:https://www.haomeiwen.com/subject/swrwyhtx.html