You can use
sed to remove the rest - everything starting with the last slash:
mpc -f %file% | head -1 | sed 's:/[^/]*$::'
/[^/]*$ matches a slash, followed by any characters except slash, up to the end of the line. It is replaced by the empty string.
head -1 ignores some status output following the line we want - but see below for how to not pring them in the first place.
In case you are not used to sed, the command may look unusual because of the
: used. You may have seen
sed commands with
/ as separator before, like
's/foo/bar/' - I prefer to use the separator
: for readability, as the expression contains
/ itself. Alternatively, we could escape the
/ in the expression:
's/\/[^\/]*$//' - that does not make it more readable.
The way you use it with
mpc gives you two additional status lines. The option
-q for quiet switches off all the output. You need to combine
-q with explicitly printing the current playing file to get the line yon need, and nothing more:
mpc -q -f %file% current | sed 's:/[^/]*$::'
Instead of removing everything starting with the last slash, one could explicitly print everything before the last slash:
mpc -q -f %file% current | sed 's:^\(.*\)/.*$:\1:'
That matches any characters that are followed by a
/ and any other characters; It matches as much as possible, so that the
/ that is matched will be the last one.