Removing filename prefixes and suffixes from my mp3’s THE EASY WAY

 Recently, I’ve been using the subreddit r/opendirectories to find music. But one of the things that bothers me about some of the music files I find is that there are often added words to the file names that clash with how I want to organize my music.

Like today, where I came across the Tame Impala discography and every single file looks something like this

'VidoDido_Tame.Impala_Disciples - Currents.mp3'
'VidoDido_Tame.Impala_Eventually - Currents.mp3'
'VidoDido_Tame.Impala_Gossip - Currents.mp3'
'VidoDido_Tame.Impala_Let It Happen - Currents.mp3'
'VidoDido_Tame.Impala_Love Paranoia - Currents.mp3'
'VidoDido_Tame.Impala_Nangs - Currents.mp3'
'VidoDido_Tame.Impala_New Person, Same Old Mistakes - Currents.mp3'
'VidoDido_Tame.Impala_Past Life - Currents.mp3'
'VidoDido_Tame.Impala_Reality In Motion - Currents.mp3'
'VidoDido_Tame.Impala_The Less I Know The Better - Currents.mp3'
'VidoDido_Tame.Impala_The Moment - Currents.mp3'
'VidoDido_Tame.Impala_Yes I’m Changing - Currents.mp3'
/Tame Impala$ find -name "*.mp3" | wc -l
77

Every one of the 77 mp3 files here is flanked by ugly looking VidoDido_Tame.Impala_ and then the album name. I just want the track name please! And I absolutely do not want to manually rename all these files.

Let’s do it

Using a recursive function in bash, paired with the sed command, we can do this no sweat

I’ll break this down:

for i in *.mp3; do mv "$i" "$(echo "$i" | sed -e "s/^"VidoDido_Tame.Impala_"//" -e "s/"-\ Currents.mp3"//")"".mp3"; done

in the folder I’m in, use all mp3 files as input

for i in *.mp3;

with the input, move it to here

do mv "$i" "$(echo "$i" | sed -e "s/^"VidoDido_Tame.Impala_"//" -e "s/"-\ 

and “here” will be our input, but altered by “sed”

do mv "$i" "$(echo "$i" | sed -e "s/^"VidoDido_Tame.Impala_"//" -e "s/"-\ 

in sed, remove the prefix “VidoDido_Tame.Impala_” and then remove the suffix “- Currents.mp3”. And notice how I include the .mp3 with it. I add the .mp3 back in to the new file name at the end.

sed -e "s/^"VidoDido_Tame.Impala_"//" -e "s/"-\ Currents.mp3"//")"".mp3"; done

When renaming the files, I progressed album-by-album. I found this easier than having sed remove the hyphen that follows the track name and everything after it .

I ran in to some issues too, like when I forgot to use escape characters on an album name that had multiple words in it.
The command accidentally erased that directory, but since I made a backup before I started, it could just be copied right back into the directory.


And then I also had some trouble moving to a directory that ended in (EP). But I figured it out eventually 😅


When it was all said and done, I did a final check to see if I missed anything. Only the correct names appeared! Success

Really pleased with the outcome! I took my folder and uploaded back to my Synology NAS so I can listen to my music using the Audio Station app.





Comments