Use Id3 tags to rename mp3’s

This is a use at your own risk bash script I wrote to grab the artist and title from an mp3’s id3 tags using exiftool. The script prompts the user to decide it they want to keep the old file name or rename it with the format: .mp3.

I’ve written a lot of shell scripts but never posted one for others to use so, users, ye be warned!
Be sure to leave a comment it you try it out, I’d love to get some feedback on this. Thanks.

Here’s a listing of my script:

#!/bin/bash

# id3_convert.sh — Written by Christian Benedict — 11/27/2010
# Dependencies — exiftool, .mp3 files only at this time
# Check if $1 begins with “-” than translate it as a parameter. -f file mode (default), -d directory mode, -n no prompt to skip empty id3 files, –help
# I may add ability to edit id3 artist and title in the future.

SAVEIFS=$IFS
$IFS=$(echo -en “\n\b”)
convert() {
filetoconvert=$1
echo “Old File Name: “$filetoconvert
titlestring=`exiftool -title “$filetoconvert” `
artiststring=`exiftool -artist “$filetoconvert” `

artist=`echo $artiststring | awk ‘{ $1 = “” ; $2 = “” ; print }’`
title=`echo $titlestring | awk ‘{ $1 = “” ; $2 = “” ; print }’`
# remove leading and trailing white space
artist=`echo $artist | awk ‘{gsub(/^[\t]+|[\t]+$/,””);print}’`
title=`echo $title | awk ‘{gsub(/^[\t]+|[\t]+$/,””);print}’`

# Log a list of files with no id3 tag info. Maybe make the script interactive or with an interactive option to decide re-name Y/N .

newname=”$artist-$title.mp3″
echo “New File Name: $newname”
if [ -n “$artist” ] ; then
echo
echo -n “Rename (r), Skip (s) or Quit (q):”
read RENAME
echo
if [ $RENAME = “r” ] ; then
echo “Renaming File: $filetoconvert –> $newname”
mv “$filetoconvert” “$newname”
sleep 1
clear
else
if [ $RENAME = “q” ] ; then
echo “You quit early”
echo
exit
fi
echo “Skipping $filetoconvert”
sleep 1
clear
fi
else
echo
echo “Insufficient id3 info to build new file name. – ENTER to skip and continue.”
read skip
clear
fi
}

clear

if [ -n “$1” ] ; then
echo
filetoconvert=$1
convert “$filetoconvert”
else
yn=” ”
echo “File name not specified. Do you want to switch to directory read mode? y/n”
read yn
if [ “$yn” != “y” ] || [ -z “$yn” ] ; then
echo ; echo “Goodbye” ; echo
exit
else
clear
for musicfiles in *.mp3
do
echo “Current Directory Read Mode”
echo
filetoconvert=$musicfiles
convert “$filetoconvert”
done
fi
fi

echo “FINISHED”
IFS=$SAVEIFS