#!/bin/bash
#
# My mp3s are always heniously unsorted and blowed if im gonna sit there spending hours sorting them
# Call me lazy or something but this definatly helps (even though i spent a couple days writing this and working out bugs)
#
# WARNING – THIS PROGRAM RELIES ON ID3 TAGS!
#
# There are a couple programs to get the ID3 tags sorted out available
# easytag, juk, rhythembox etc (those are all gui ones)
#
# Make sure mp3info is installed ( http://ibiblio.org/mp3info/ )
# just ./runthis and hey presto (make sure you edit where your mp3s are of course first)
#debug
#set -x
#The Dir where all your mp3s are
DIR=”/other/mp3s”
#The final destination dir for the mp3s – No trailing / needed
DESTINATION=”/other/mp3s”
#This is the thing that seperates fields (some people prefer _ some prefer a space however SPACES BREAK THINGS! use ‘1’ if you want spaces)
SPACER=”_”
#try not too edit to much below this
if [ -e moving.log ];then
rm -rf moving.log
fi
if [ ${SPACER} = “1” ];then
SPACER=”[:space:]”
fi
if [ $1 ];then
find ${DIR} -type f -name \*.[mM][Pp]3 | while read i
do
FINALDEST=”${DESTINATION}”
ARTIST=`mp3info -p %a “$i” | tr [:punct:][:blank:] ${SPACER} `
ALBUM=`mp3info -p %l “$i” | tr [:punct:][:blank:] ${SPACER} `
GENRE=`mp3info -p %g “$i” | tr [:punct:][:blank:] ${SPACER}`
SONGNAME=`mp3info -p %t “$i” | tr [:punct:][:blank:] ${SPACER}`
TRACKNUM=`mp3info -p %n “$i”`
FILENAME=`mp3info -p %f “$i” | tr [:punct:][:blank:][mM][Pp]3 ${SPACER} `
# sort the artists
function artistpath {
if [ ! “${ARTIST}” ];then
FINALDEST=”${FINALDEST}/Unknown/”
else
FINALDEST=”${FINALDEST}/${ARTIST}/”
fi
}
function albumpath {
#sort the albums
if [ ! “${ALBUM}” ];then
FINALDEST=”${FINALDEST}/Unknown/”
else
FINALDEST=”${FINALDEST}/${ALBUM}/”
fi
}
if [ $1 == “1” ];then
artistpath
albumpath
elif [ $1 == “2” ];then
albumpath
artistpath
elif [ $1 == “3” ];then
artistpath
elif [ $1 == “4” ];then
albumpath
fi
FINALDEST=`echo ${FINALDEST} | sed s/${SPACER}${SPACER}*/${SPACER}/g`
echo mkdir -p “${FINALDEST}”
echo mkdir -p “${FINALDEST}”>>moving.log
mkdir -p “${FINALDEST}”
unset NEWNAME
if [ “$2″ ];then
NEWNAME=`echo $2 | sed s/TRACKNUM/$TRACKNUM/g | sed s/ALBUM/$ALBUM/g | sed s/ARTIST/$ARTIST/g | sed s/SONGNAME/$SONGNAME/g`
else
NEWNAME=”${TRACKNUM}-${ALBUM}-${ARTIST}-${SONGNAME}”
fi
if [ “${TRACKNUM}” == “” ] && [ “${ALBUM}” == “” ] && [ “${ARTIST}” == “” ] && [ “${SONGNME}” == “” ];then
echo “Songname is non existant (no id3 tag?) – resorting to ${FILENAME} $i”
echo “Songname is non existant (no id3 tag?) – resorting to ${FILENAME} $i”>>moving.log
NEWNAME=”${FILENAME}”
fi
NEWNAME=`echo ${NEWNAME} | sed s/–*/-/g | sed s/^-// | sed s/^${SPACER}// `
FINALDEST=”${FINALDEST}${NEWNAME}.mp3″
FIXDEST=`echo ${FINALDEST} | sed s/${SPACER}${SPACER}*/${SPACER}/g `
echo moving $i to ${FIXDEST}
echo moving $i to ${FIXDEST} >>moving.log
mv -u “$i” “${FIXDEST}”
FINALDEST=”${DESTINATION}”
unset FIXDEST
done
# cleaning up empty dirs
#add this
#for i in `find ./ -type d ` ; do if [ “`find $i -type f`” ];then echo checking $i ; else echo $i is empty;rm -r “$i” ; fi ; done
for EMPTYDIR in ${DESTINATION}/*
do
if [ -d “${EMPTYDIR}” ];then
if [ “`find ${EMPTYDIR} -type f`” ];then
echo Checking ${EMPTYDIR} for cleanup
else
echo ${EMPTYDIR} is empty and being removed
echo ${EMPTYDIR} is empty and being removed >>moving.log
rm -r “${EMPTYDIR}”
#find “${EMPTYDIR}”
fi
else
echo ${EMPTYDIR} is a file
fi
done
# main else
else
echo “Useage: $0 {1|2|3|4} [PATTERN] ”
echo “1 sort by artist then album”
echo “2 sort by album then artist ”
echo “3 sort by artist only”
echo “4 sort by album only”
echo
echo “[PATTERN] – defaults to TRACKNUM-ARTIST-ALBUM-SONGNAME”
echo “ARTIST – Artist”
echo “ALBUM – Album Name”
echo “TRACKNUM – TrackNumber”
echo “SONGNAME – Songname”
fi
# Yes i realize some of the code is horiffic but it did the job at the time ;]
# Liz