LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Bash script for sorting and renaming multiple mp3 files by id3 tags (http://www.linuxquestions.org/questions/linux-general-1/bash-script-for-sorting-and-renaming-multiple-mp3-files-by-id3-tags-602105/)

simonloach 11-24-2007 11:18 AM

Bash script for sorting and renaming multiple mp3 files by id3 tags
 
Hi I have several thousand mp3 files that I would like to sort into directories and rename so that they are all like:

/music/mp3/Artist-Album-Year/TrackNumber-Artist-TrackName.mp3

I have found a program that will return the data contained in the id3 tags (id3tool) and display it like:

./id3tool 10\ So\ Here\ We\ Are.mp3
Filename: 10 So Here We Are.mp3
Song Title: So Here We Are
Artist: Bloc Party
Album: Silent Alarm
Track: 10
Year: 2005
Genre: Rock (0x11)

I just need to find out how to create the directories and rename the files from the output of id3tool.

Thanks for any help.

ilikejam 11-24-2007 01:23 PM

Hi.

Code:

#!/bin/bash
TITLE="`id3tool "$1" | grep '^Song Title:' | awk '{ for (i=3;i<=NF;i++) { printf $i; printf " " } }'`"
ARTIST="`id3tool "$1" | grep '^Artist:' | awk '{ for (i=2;i<=NF;i++) { printf $i; printf " " } }'`"
ALBUM="`id3tool "$1" | grep '^Album:' | awk '{ for (i=2;i<=NF;i++) { printf $i; printf " " } }'`"
YEAR="`id3tool "$1" | grep '^Year:' | awk '{ for (i=2;i<=NF;i++) { printf $i; printf " " } }'`"
TRACKNUM="`id3tool "$1" | grep '^Year:' | awk '{ print $2 }'`"

install -D "$1" /music/mp3/"$ARTIST-$ALBUM-$YEAR"/"$TRACKNUM-$ARTIST-$TITLE".mp3

does what you're looking for.

fancybone 05-15-2009 11:25 AM

Thanks, ilikejam, I modified your script to work with id3info instead of id3tool; I don't think id3tool supports id3v2.

Code:

#!/bin/bash
TITLE="`id3info "$1" | grep '^=== TIT2' | sed -e 's/.*: //g'`"
ARTIST="`id3info "$1" | grep '^=== TPE1' | sed -e 's/.*: //g'`"
ALBUM="`id3info "$1" | grep '^=== TALB' | sed -e 's/.*: //g'`"
YEAR="`id3info "$1" | grep '^=== TYER' | sed -e 's/.*: //g'`"
TRACKNUM="`id3info "$1" | grep '=== TRCK' | sed -e 's/.*: //g'`"

echo "$ARTIST $TITLE $ALBUM $YEAR $TRACKNUM"

mv $1 "$ARTIST - $ALBUM - $TRACKNUM - $TITLE.mp3"


colucix 05-15-2009 11:42 AM

You may be interested in eyeD3, which let you rename the files specifying which tags you want to insert. For example:
Code:

eyeD3 --rename="%A - %a - %n - %t" $file

simonloach 05-16-2009 05:55 AM

Thanks for the responses, but I think I solved this problem quite a long time ago. I'm sorry I didn't respond I must have completely forgot about this. Hopefully it will be of use to other people.

dantheperson 12-29-2011 04:28 PM

and if you save that as reorg.sh you can process all files in a directory with:

for file in *; do reorg.sh $file; done

dantheperson 12-29-2011 05:24 PM

Also, to cope with files that have the album track count


#cope with track count e.g. tracknum=10/11
CUTTRACKNUM="`echo $TRACKNAME | cut -c1-2`"

aix_bloke 09-08-2012 01:03 PM

Using the suggestions from colucix:
Quote:

eyeD3 --rename="%A - %a - %n - %t" $file
and the idea from dantheperson:
Quote:

for file in *; do reorg.sh $file; done
I have the following:
Code:

#!/bin/bash
# Next line added to cope with spaces in filenames
IFS="
"
for file in *.mp3
 do
    eyeD3 --rename="%A - %a - %n - %t" $file
 done

Hope this helps someone as the previous posts got me started on what I wanted to achieve.

Kevin

koala 02-16-2013 09:07 AM

Full script to update ALL tags
 
Hi,

The script below asumes you have your music in directories in the form ".../Artist/Album/song-file.mp3", and you run the script from the Artist directory.

Hope you all find it usefull.

Cheers,

Kev

#! /bin/bash
for ART in *
do
if [ -d "$ART" ]
then
echo "++++++++++++++++++++++++++++++++++++++++"
echo "Artist: "$ART
echo "++++++++++++++++++++++++++++++++++++++++"
cd "$ART"
for SUB in *
do
if [ -d "$SUB" ]
then
cd "$SUB"
ALBUM=`pwd | awk -F\/ '{print $NF}'`
echo "Album: " $ALBUM
echo "========================"
for SONG in *mp3
do
TITLE=`echo $SONG | cut -d\. -f1 | tr "_" " "`
echo $TITLE
id3tag --song="\"$TITLE\"" $SONG
id3tag --album="\"$ALBUM\"" $SONG
id3tag --artist="\"$ART\"" $SONG
done
cd ..
fi
echo " "
done
cd ..
echo " "
fi
done
exit 0


All times are GMT -5. The time now is 05:26 AM.