Create directory in date like structure

sachin
edited September 2021 in Linux

I have so many files name like 8888_20210828-000547_9123456789_ABCDEFD_A1099-all.mp3 and wants to create a directory structure 2021/08/28/ABCDEFD/ which I extract from given path and move these files into created directories using shell script

Tagged:

Answers

  • sachin
    edited September 2021

    Create a shell script let's say move_files.sh with the following snippet

    function filemove() {
        FILE_NAME=$(basename "$1")
        if [[ "$FILE_NAME" =~ ^.+\_[0-9]{8}\-.+_.+_.+-.+\.mp3$ ]]; then
            FILE_DIR=$(echo $1 | awk -F'[_-]' '{print substr($2,1,4) "/" substr($2,5,2) "/" substr($2,7,9) "/" $5 "/";}');
            echo "$FILE_NAME : >> : $FILE_DIR"
            echo "---------------------------";
            echo -e "creating directory:: \e[1;32m $FILE_DIR\e[0m";
            mkdir -p "$FILE_DIR";
            echo -e "moving file:: \e[1;32m $FILE_NAME to $FILE_DIR\e[0m";
            mv -f "$FILE_NAME" "$FILE_DIR";
        else
            echo -e "\e[1;31m ::---------------Invalid file name $FILE_NAME---------------:: \e[0m";
        fi
        echo ""
    }
    
    export -f filemove
    find ./ -maxdepth 1 -type f -name "*.mp3" -exec bash -c "filemove \"{}\"" \;
    

    Now place this file inside the directory containing mp3 files
    and finally form the terminal execute the shell file
    ./move_files.sh

  • sachin
    edited September 2021
    function filemove() {
        FILE_NAME=$(basename "$1");
        # FILE_PATH=$(dirname "$1");
        FILE_PATH="/home/username/";
        echo -e "\e[1;32mFILE_NAME :: $FILE_NAME :: \e[0m";
        echo -e "\e[1;32mFILE_PATH :: $FILE_PATH :: \e[0m";
    
        if [[ "$FILE_NAME" =~ ^.+\_[0-9]{8}\-.+_.+_.+-.+\.mp3$ ]]; then
            FILE_DATE=$(echo "$FILE_NAME" | awk -F'[_-]' '{print substr($2,1,4) "-" substr($2,5,2) "-" substr($2,7,9);}');
            FILE_MONTH=$(date -d "$FILE_DATE" +%b);
            FILE_MONTH_FULL=$(date -d "$FILE_DATE" +%B);
            FILE_YEAR=$(date -d "$FILE_DATE" +%Y);
            FILE_YEAR_SHORT=$(date -d "$FILE_DATE" +%y);
            FILE_DAY=$(date -d "$FILE_DATE" +%d);
            FILE_DIR=$FILE_PATH$(echo "$FILE_NAME" | awk -F'[_-]' -v FILE_MONTH_FULL=$FILE_MONTH_FULL '{print substr($2,1,4) "/" FILE_MONTH_FULL "/" substr($2,7,9) "/" $(NF-2) "/";}');
            echo "$FILE_DATE : >> : $FILE_MONTH : >> : $FILE_MONTH_FULL : >> : $FILE_YEAR : >> : $FILE_YEAR_SHORT : >> : $FILE_DAY";
            echo "$FILE_MONTH_FULL : >> : $FILE_DATE : >> : $FILE_NAME : >> : $FILE_DIR";
            echo "---------------------------";
            echo -e "creating directory:: \e[1;32m $FILE_DIR\e[0m";
            mkdir -p "$FILE_DIR";
            echo -e "moving file:: \e[1;32m $1 to $FILE_DIR\e[0m";
            mv -f "$1" "$FILE_DIR";
        else
            echo -e "\e[1;31m ::---------------Invalid file name $FILE_NAME---------------:: \e[0m";
        fi
        echo ""
    }
    
    export -f filemove
    find /home/username/ -maxdepth 3 -type f -name "*.mp3" -exec bash -c "filemove \"{}\"" \;
    

    This will generate path /home/username/2021/September/24/ABCDEFD/

  • Bash date format options

    Below is a list of common date format options with examples output. It works with the Linux date command line and the mac/Unix date command line.

    Date Format Option Meaning Example Output
    date +%c locale’s date time Sat May 9 11:49:47 2020
    date +%x locale’s date 05/09/20
    date +%X locale’s time 11:49:47
    date +%A locale’s full weekday name Saturday
    date +%B locale’s full month name May
    date +%m-%d-%Y MM-DD-YYYY date format 05-09-2020
    date +%D MM/DD/YY date format 05/09/20
    date +%F YYYY-MM-DD date format 2020-05-09
    date +%T HH:MM:SS time format 11:44:15
    date +%u Day of Week 6
    date +%U Week of Year with Sunday as first day of week 18
    date +%V ISO Week of Year with Monday as first day of week 19
    date +%j Day of Year 130
    date +%Z Timezone PDT
    date +%m Month of year (MM) 05
    date +%d Day of Month (DD) 09
    date +%Y Year (YYYY) 2020
    date +%y Year (YY) 20
    date +%H Hour (HH) 11
    date +%H Hour (HH) in 24-hour clock format 11
    date +%I Hour in 12-hour clock format 11
    date +%p locale’s equivalent of AM or PM AM
    date +%P same as %p but in lower case am

    You can get the full list of supported date format specifiers using the man date or man gdate commands.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!