Delete a currently playing song in mpd

Published: by

  • Categories:

So here a quick script to delete a currently playing song in mpd music player daemon of which I am a big fan!

#!/bin/bash

kdialog --title "Removing the song" --yesno "Do you really want to delete the song \n$(mpc | head -n 1)?"
 
#If Yes, then 0 is returned, else 1
reply=$(echo $?)

if [[ reply -eq 0 ]];then
        song_to_remove=$(mpc | head -n 1)
        playlist_pos=$(mpc -f %position% | head -n 1)
        #Delete the song
        rm "$(mpc -f %file% | head -n 1 | sed 's/^/\/media\/Data\/mpdLibrary\//')"
        #Remove the song from playlist
        mpc del $playlist_pos
        #Write to log file
        echo "[`date`] -> --$song_to_remove-- is now deleted..." >> ~/.mpdremove.log
fi

Here, I have my music directory as /media/Data/mpdLibrary, change it according to your needs.. Save the above script in your home directory with name, lets say removesong.sh and then make it executable by

$chmod +x removesong.sh

You can assign the script to Shortcut keys according to your desktop environment. (In KDE 4.6, its in System Settings->Shortcuts & Gestures).