Moving files from several directories into parent

I am fairly new to bash(but am proficient in C++), and have only completed a few simple scripts. This is my first script that I actually need to do a serious task.

All of my audiobooks are stored in traditional MP3 format: Music/Artist/Album/.mp3 (which in this case is Music/Author/Book/Disc/
I need to move all of the mp3s into their respective Book directories, rename them to Book1.mp3, Book2.mp3, etc , and delete the Disc directories. I have figured out move the contents of a single Disc into it's parent and rename them correctly, and how to delete the directory.

My question is this: How do I parse through the different Disc directories? I am currently running the script from the Disc directory, but would like to run it from the Author directory. I'm thinking that I'm gonna have to use some nested for loops, but can't figure out how to make them work.

Here's is my crude idea:

let num=1

for Book in *; do
       for Disk in "$Book"/*; do

               for song in "$Book"/"$Disc"/*.mp3; do
                       mv "$song" "$Book"/$num"$Book".mp3
                        let num=$num+1
               done
               rmdir "$Book"/"$Disc"
       done
done


I think that my problem lies in the first two for loops. I know that those work for files, but I'm not sure about directories.

Thanks in advance for the help!

---------- Post updated at 02:01 PM ---------- Previous update was at 11:55 AM ----------

Nevermind, I figured it out:

#!/bin/bash

let num=1

for book in *
do
    for disc in "$book"/*
    do
        for file in "$disc"/*.mp3
        do
            if [ $num -lt 10 ]; then
                mv "$file" "$book"/"$book"0"$num".mp3

            else
                mv "$file" "$book"/"$book$num".mp3
            fi
            let num=$num+1
        done
        rm "$disc"/*
        rmdir "$disc"
    done
done
1 Like