Use the shell to move the files in the YYYY-MM-DD directory to YYYY-MM/DD

1. Write a shell script dir10.sh to create the directory YYYY-MM-DD 365 days before the current date, and there are 10 files $RANDOM.log below

vim dir10.sh

#!/bin/bash

for i in {1..365} ;do
        DIR=`date -d "-$i day" +%F`
        mkdir /data/test/$DIR

    for j in {1..10};do
        
        touch /data/test/$DIR/$RANDOM.log
 done 
        cd ..
done

Run screenshots (the tree command system is not available by default, you need to install yum install tree -y)

2. Move the files in the YYYY-MM-DD directory to YYYY-MM/DD

vim mv_dir10.sh

#!/bin/bash

DIR=/data/test
cd $DIR
for DIR in * ;do

        YYYY_MM=`echo $DIR|cut -d'-' -f1,2`
        DD=`echo $DIR|cut -d'-' -f3`
        [ -d $YYYY_MM/$DD ] || mkdir -p $YYYY_MM/$DD
        mv $DIR/* $YYYY_MM/$DD
done

----------------------------------------------------------
#将YYYY-MM-DD目录移动到YYYY-MM下

#!/bin/bash

DIR=/data/test
cd $DIR
for DIR in * ;do

        YYYY_MM=`echo $DIR|cut -d'-' -f1,2`
        [ -d $YYYY_MM ] || mkdir  $YYYY_MM
        mv $DIR $YYYY_MM
done

 

 

Guess you like

Origin blog.csdn.net/l_liangkk/article/details/114266741