Tidy up mp3 files in per-artist directory

Tidy up mp3 files in per-artist directory

There is quite an amount of work behind my radio stream. I download lots of new music file everyday, and at some point, I need to transfer all these mp3 files to my NAS, in order to add the new songs to the pool of songs used by the Pi Radio.

On my NAS, I arrange all my music by artist. So I have a list of directory per artist. I don't necessarily arrange the music by Album, even if I could; it demands too much time for me.

Once my "Download" directory is filled with too much new mp3 files, I spend some time to move every mp3 file to a directory that has the name of the artist of the song. Thus I need to first check if the artist is already present on my NAS, and if not, create the directory myself to be able to move the song in that new folder. So one day I though "it's so boring, let's automize!"

Here is a shell script that will move all mp3 files in a directory named by the Artist name.

find . -name "*.mp3" -type f -printf "%f\n" |
while read filename
do
    music=${filename%.mp3}
    artist=${filename% -*}
    if [ -d "$artist" ]; then
        mv "$filename" "$artist"
    else
        mkdir -pv "$artist"
        mv "$filename" "$artist"
    fi
done

The script is pretty simple to read. We list all mp3 files in the current directory where we run the script. For each file, we gather the artist name from the filename. So "Avicii - Hey Brother.mp3" will become "Avicii". And the folder will be created with this artist name.

This implies that all your files are correctly named like this "ARTIST - SONG.mp3"

If the folder exists, we just move the file in it. Otherwise, we create the directory and move the file right after.

Simple af!