$ mpc search title "when I was your man" 
Bruno Mars/Unorthodox Jukebox/06 Bruno Mars - When I Was Your Man.mp3

How do I play the song? I've tried piping it to mpc play but no luck.

$ mpc search title "when I was your man" | mpc play

If you look at what mpd play is supposed to do:

play <position>
  Starts playing the song-number specified. If none is specified, plays number 1.

you'll notice that it doesn't really make sense to specify a filename as position.

I'm not sure there's an easy way to do what you want with mpc because it doesn't support all commands MPD does (like addid).

If you don't want to keep your playlist, you can do

mpc clear; mpc search title "when I was your man" | mpc add; mpc play

which clears the playlist, adds the song(s) you want and plays the first song in the playlist.

The best I could come up with to play add a song to a playlist without clearing it and playing the song is

title="your song title here"
mpc search title $title  | mpc add
mpc playlist -f '%position% %title%' | grep -i $title | head -n1 | cut - -d " " -f 1 | xargs mpc play
  • You can get the position using mpc status | sed -n -e '/#/ s/^.*#\([[:digit:]]*\)\/.*$/\1/p'. – Mateen Ulhaq Sep 9 '19 at 6:03

mpc play expects a position as argument, not a filename. A position in a playlist to be precise. You first have to add the file to a playlist, and then tell mpc to play that song from the playlist. But add will add the new song at the end of the playlist. To play the newly added song you have to know how long the playlist is.

Here are some suggestions how you can add a song and start playing it:

  1. Clear the playlist first:

    mpc clear
    mpc search title "when I was your man" | mpc add
    mpc play
  2. Insert as next and play next:

    mpc search title "when I was your man" | mpc insert
    mpc next
  3. Determine length of playlist to play last:

    mpc search title "when I was your man" | mpc add
    mpc play $(mpc playlist | wc -l)

There is a simpler way than the accepted answer (in case the song is already in your playlist):

mpc searchplay filename "$(mpc search title 'when I was your man')"

If no result is found, mpc will simply play the first song in your playlist.

If you don't want the output of mpc, just append 2>&1 >/dev/null.


Instead of using mpc search function, i've seen what you want done with a 'jump to song' script: http://th0th.me/log/mpd-music-player-daemon-jump-to-song/

this uses mpc to get a full playlist with 'position', greps this list for the search keywords and sends the corresponding 'position' marker to mpc to play


This answer is for if you are adding more than one track (e.g. album) to the end.

We can obtain positions within now playing:

position="$(mpc status | sed -n -e '/#/ s/^.*#\([[:digit:]]*\/[[:digit:]]*\).*$/\1/p')"
curr="$(<<< "$position" cut -d/ -f1)"
length="$(<<< "$position" cut -d/ -f2)"

Then, simply insert your search result:

mpc search title "when I was your man" | mpc insert

And play from the beginning of the end:

mpc play "$((length + 1))"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.