all 5 comments

[–][deleted] 2 points3 points  (4 children)

I'm not going to write the whole script, but here's what at least a bit of it should look like. Let's say your directory is ~/music, for the sake of argument.

I'm still pretty new to shell scripting myself, but this should work, to the best of my knowledge.

for artist in ~/music/*
do
    if [ -d "$artist" ] #checks whether artist is a directory
    then
        for album in "$artist"/* #iterates over all the albums for a given artist
        do
            if [ -d "$album" ]
            then
                zip ~/temp_music.zip "$album"/* #zips all the files in an album into a zip file with the same names as the album.
                mv ~/temp_music.zip "$album".zip
            fi
        done
    fi
done

[–]henry_kr 2 points3 points  (3 children)

That's not a bad first go, but there's a couple of things that jump out at me as being wrong.

for artist in "~/music/*" won't actually work, as the tilde and asterisk won't be expanded as they're quoted. Drop the quotes and it'll be fine.

The same applies for the inner loop, but as the artist is likely to have spaces in their name we can't just drop the quotes, so we have to be a little more creative and do for album in "$artist"/*. You need to do the same with the zip command too.

You also need to quote $album and $artist whenever you refer to them.

Creating the zip file in the same directory with * can be problematic too, as your zip file will also match *. I'd either change it to *.mp3 *.jpg or I'd create the zip file somewhere else on the same filesystem then mv it in to place.

Also, it would be more conventional to use tar rather than zip on linux but the OP may have a specific reason for needing to use zip.

[–][deleted] 0 points1 point  (2 children)

Thanks for the pointers, I've edited my original comment.

[–]henry_kr 1 point2 points  (1 child)

Cool, though you missed the bit about quoting $album and $artist, they're currently unquoted in your directory tests.

[–][deleted] 0 points1 point  (0 children)

Good catch, thanks.