To watch all videos with mpv in predefined sorted order one can do

mpv /path/to/videos/*

because mpv can accept multiple files as its argument and play them one after another.

I would like to pass to mpv those files but randomly ordered, so each time I start watching it comes up with something unexpected.

Here what I've tried so far

ls /path/to/videos/* | sort -R | while read file; do mpv $file; done

This variant does not satisfy my needs, since it starts new instance for each video and new window always gets focused.


Use a subshell to collect the randomized lines back into arguments to mpv. Remove debugging echo if trial runs look good.

echo mpv $(print -l /path/to/videos/* | sort -R)

(Also there is no need to use ls as that's more expensive than simply printing the results of the glob expansion linewise with print -l.)

Internal to ZSH, try a clever glob trick to shuffle the list:

echo mpv *(oe:REPLY=\$RANDOM:)
  • @tarashypka generally I aim to not have characters in filenames that complicate life in the shell.
    – thrig
    Jan 28 '17 at 1:01
  • It works well for file names without whitespaces. For another case I've tried mpv $(printf '%q\n' /path/to/videos/* | sort -R), but regardless it created espace sequences, error is still the same. Is it possible to improve it or should I constantly rename the files to eliminate whitespaces etc?
    – tarashypka
    Jan 28 '17 at 1:02
  • Second command works as charm and looks as magic. It's definitely time to learn more about zsh. Thanks.
    – tarashypka
    Jan 28 '17 at 1:06

mpv has an option built-in for shuffling the order of files played (--shuffle)

You could use mpv --shuffle /path/to/videos/*

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.