-   Linux - General (
-   -   Bash script for sorting and renaming multiple mp3 files by id3 tags (

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:


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



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.


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'`"


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:

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 you can process all files in a directory with:

for file in *; do $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:

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

for file in *; do $file; done
I have the following:

# Next line added to cope with spaces in filenames
for file in *.mp3
    eyeD3 --rename="%A - %a - %n - %t" $file

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


koala 02-16-2013 09:07 AM

Full script to update ALL tags

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.



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

All times are GMT -5. The time now is 10:10 PM.