How to get a random filename from a directory and using it within a command?

play ~/Music/audio0.flac repeat -

A command for playing a single specific audio file on repeat.

play ~/Music/${random_audio_file} repeat -

A pseudo command for playing a single random audio file from the given directory (~/Music/), also on repeat.


How to do the latter as a real working command?

2 Answers

You can leverage the shuf command for that. Try:

play "$(find ~/Music -name '*.flac' | shuf -n 1)" repeat -

The double quotes are necessary in case your filenames may contain spaces.

This command will get a random .flac file and play it using omxplayer. Modify the command to suite your player.

find . -type f -name '*.flac' | shuf -n 1 | xargs -d "\n" omxplayer

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like