0

I have many mp3 songs in a directory and I want to sort them by artist such that each artist name is a directory containing that artist's music.

7
  • 2
    ... Well... I'm afraid this question is terribly unclear, you should consider rewriting it so that you can get some answers. It is very hard to answer something that makes no sense. Thanks.
    – Josiah
    Oct 5, 2012 at 18:06
  • Is the artist's name in the filename? Is there an unambiguous way to extracting the artist name from the filename or from the file content?
    – wallyk
    Oct 5, 2012 at 18:18
  • This does not belong on stackoverflow, the OP had not asked for a "programmatic" solution that came from a subsequent edit. I have changed it to batch. It is perfectly on topic here, see my answer below.
    – terdon
    Oct 5, 2012 at 18:34
  • @terdon The reason this seems to belong on stackoverflow is because the user had tagged this question as unix-programming.
    – Ganesh R.
    Oct 5, 2012 at 18:37
  • 3
    If this question is about simple Unix shell scripting and usage (and it really looks like it), it is on topic on Super User. But please also note that existence of tags does not make tagged questions on topic.
    – Daniel Beck
    Oct 5, 2012 at 19:08

3 Answers 3

3

This assumes your mp3 tags are in id3v2 format. id3v1 formats things a little differently. I recommend something like mediainfo for that. This example uses id3v2

for file in *.mp3; do
    IFS=$':'
    artist="$(id3v2 -l "${file}" | grep "TPE1" | awk '{ print $2 }')"
    # Your artist name is now stored in $artist.
    # This example just creates an artist directory and moves the file into it.
    # Terrible way to organize music, but this is a demonstration!
    [[ ! -d "${artist}" ]] && mkdir -p "${artist}"
    mv -f "${file}" "${artist}"
done

A real solution is to use something like MusicBrainz Picard.

3
  • What if there are multiple files from the same artist? Will this handle that scenario?
    – Ganesh R.
    Oct 5, 2012 at 18:28
  • It will move ALL songs by the same artist into the same artist folder. This assumes that all files are already tagged properly. Oct 5, 2012 at 18:30
  • Read the comments in that script. Don't just run what I posted and call it good. Oct 5, 2012 at 18:33
0

I am not sure what OS you are on, but since your question was tagged as Unix-programming I will assume you are on an *ix. So, if you can install id3tool you could run the following BASH command line:

find . -name "*.mp3" | while read n; do 
    dir=`id3tool "$n" | grep Artist | cut -f 3 | sed 's/\s*$//'`; 
    mkdir -p "$dir"; 
    mv "$n" "$dir"/"$n" ; 
done

EXPLANATION:

This little script will loop through each of your mp3 files, extract the artist's name using id3tool and save it in the variable $dir. It will then create a directory with the artist's name (unless it already exists) and proceed to move the file into that directory.

You can also do this in a GUI way by using music players such as amarok that offer this feature (or at least it used to). There are also various id3 editors, just search through your favorite software source, at least some of which should also be able to do this.

2
  • 1
    And, of course, I now see that @UtahJarhead has given essentially the same answer while I was writing mine. Oh well :)
    – terdon
    Oct 5, 2012 at 18:45
  • For the record, it's recommended to not use ` in bash scripts. They don't nest very well and it's easier to read $(). Oct 5, 2012 at 19:58
0

I would do this in Python, with the tool pointed out in this answer on Stack Overflow, eyeD3.

1
  • +1 I think this belongs on StackOverflow more than StackExchange
    – Ganesh R.
    Oct 5, 2012 at 18:17

Not the answer you're looking for? Browse other questions tagged .