I have a folder with a few files named 1.mp3, 2.mp3, 3.mp3, ...

Also, I have a command which accepts an mp3 file as an input:

blablabla -i mp3/1.mp3

I would like to pass to the command random mp3 file.

blablabla -i mp3/<random int in range 1-n>.mp3


blablabla -i mp3/<get random file from mp3 folder>

I am looking for the simplest method to do it, it doesn't matter which one. Who can help?


Use blablabla -i mp3/$(( ( RANDOM % N ) + 1 )).mp3

Replace N with Max number you want to generate, or 1 Min number.

Or you can use $(shuf -i MIN-MAX -n 1) instead.

from man shuf:

-i, --input-range=LO-HI
    treat each number LO through HI as an input line
-n, --head-count=COUNT 
    output at most COUNT lines

Actually -n 1 in shuf here means only generate one random Num, you can test with -n 2 which is generating two Random Nums.

You may also wants to have this with printf if your files name has leading 0's.

printf "%03d.mp3\n" $(( (RANDOM % 100) + 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.