Cleaning up my music collection

My music collection was a mess from moving it between devices, backups and  adding to it.  I thought it was time to put Linux to work and clean up my collection. This is how I did it.


Remove unneeded files

I first had to do some clean up. to me this is just blot. Some will want to keep the album art so this might not be for all.

find . -type f -name "*.db" -exec rm -f {} \;

find . -type f -name "*.jpg" -exec rm -f {} \;

Remove Duplicates

Next I wanted to find all the duplicates so I ran this which uses md5sums  and uniq to find the duplicate files.

find . -type f -exec md5sum '{}' ';' | sort | uniq --all-repeated=separate -w 20

Fix Spaces in name and convert wma to mp3

I got tired of all the spaces in  he filenames so I decided to replace the space with a underscore.

rename ' ' '_' *

That worked grate for the folders in the directories but it didn’t cover the file in the directory so I also ran this.

for X in $(ls -1); do rename ' ' '_' $X/* ; done

What I found is that some songs had alot of spaces in  the name and the above for loop didn’t get all of them corrected. To fix the problem I wrote a bash script that would remove the space’s and convert the wma files to mp3’s. I had to  encapcalating IFS to do the rename because if not it didn’t handle all the spaces. I never tried that when running the simple for loop above but that might have fixed the problem. The comments int he script should explain what it is doing.


#Directoru to music files

#Test that we have the proper apps installed
for X in mplayer lame; do
echo “Checking $X is installed”
which $X >/dev/null 2>&1
if [ “$?” != “0” ]; then
echo -e “$X is not installed or isnt in my path.\n Goodbye”
exit 1

#Fix naming
echo -e “Fixing file names with spaces\n”
#The IFS is for files with multiple spaces in the name
#encapcalating IFS in ( ) only sets it for this loop
for X in $(find $MDIR -name ‘* *’) ; do
NEW_NAME=$(echo $X | tr ‘ ‘ _)
echo “Renameing $X”

echo -e “\nFile names fixed\nFixing wma files :)\n”

if [ -f $MDIR/audiodump.wav ]; then
rm $MDIR/audiodump.wav
if [ -f audiodump.wav ]; then
rm audiodump.wav

#Loop throught all wma’s in this dir and fix them with mplayer and lame
for X in $(find $MDIR -name “*.wma”); do
echo “Fixing $X”
mplayer -vo null -vc dummy -af resample=44100 -ao pcm:waveheader $X && lame -m s audiodump.wav -o $X
#Now fix the name
mv $X $MDIR/$(basename $X .wma).mp3
#Remove the tmp file that was created in the conversion process
if [ -f $MDIR/audiodump.wav ]; then
rm $MDIR/audiodump.wav
if [ -f audiodump.wav ]; then
rm audiodump.wav


Leave a Reply

Your email address will not be published. Required fields are marked *