Claim your Biolink Click Here
1 like 0 dislike
280 views
We have a directory consisting so many files/string like below

1234_20190817-194937_1111111111_NAME_ABC-all.mp3
111_20180312-194937_222222_FILENAME2_xyz-all.mp3

and need to break and make it different folders like below

> 2019/08/17/NAME

> 2018/03/12/FILENAME2

so need to write shell scripting using AWK command
in Education & Reference by (4.7k points) | 280 views

1 Answer

0 like 0 dislike
function filemove() {
FILE_NAME=$(basename "$1")
if [[ "$FILE_NAME" =~ ^.+\_[0-9]{8}\-.+_.+_.+-.+\.mp3$ ]]; then

FILE_DIR=$(echo $1 | awk -F'[_-]' '{print "/FOLDER/" 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 /FOLDER/ -maxdepth 1 -type f -name "*.mp3" -exec bash -c "filemove \"{}\"" \;
by (820 points)

Related questions

0 like 0 dislike
1 answer
1 like 0 dislike
1 answer
1 like 0 dislike
1 answer
0 like 0 dislike
1 answer
1 like 0 dislike
1 answer
1 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
1 like 0 dislike
1 answer

Where your donation goes
Technology: We will utilize your donation for development, server maintenance and bandwidth management, etc for our site.

Employee and Projects: We have only 15 employees. They are involved in a wide sort of project works. Your valuable donation will definitely boost their work efficiency.

How can I earn points?
Awarded a Best Answer 10 points
Answer questions 10 points
Asking Question -20 points

1,310 questions
1,471 answers
569 comments
4,809 users