tags:

views:

428

answers:

5

Once I am in the directory containing .mp3 files, I can play songs randomly using

mpg123 -Z *.mp3

But if I want to recursively search a directory and its subfolders for .mp3 files and play them randomly, i tried following command, but it does not work.

mpg123 -Z <(find /media -name *.mp3)

(find /media -name *.mp3), when executed gives all .mp3 files present in /media and its sub directories.

+1  A: 

Backticks.

mpg123 -Z `find /media -name \*.mp3`

Though if you have a lot of files, you may encounter command line length limitations.

laalto
The o/p gives 'find /media -name \*.mp3:No such file or directory'.
TwiggedToday
+4  A: 
mpg123 -Z $(find -name "*.mp3")

The $(...) means execute the command and paste the output here.

Also, to bypass the command-line length limit laalto mentioned, try:

mpg123 -Z $(find -name "*.mp3" | sort --random-sort| head -n 100)

EDIT: Sorry, try:

find -name "*.mp3" | sort --random-sort| head -n 100|xargs -d '\n' mpg123

That should cope with the spaces correctly, presuming you don't have filenames with embedded newlines.

It will semi-randomly permute your list of MP3s, then pick the first 100 of the random list, then pass those to mpg123.

Matthew Flaschen
TwiggedToday
The following command worked:find /media -name "*.mp3" | xargs -d '\n' -n10 mpg123 -Z.By '-n' option we can provide no. of arguments for a single invocation of command
TwiggedToday
Thanks Mathew for the help !
TwiggedToday
+1  A: 

Would something like this work?

find /media -name *.mp3 -print0 | xargs -0 mpg123 -Z
Seh Hui 'Felix' Leong
+1  A: 

The following works fine.

find /media -name "*.mp3" | xargs -d '\n' -n10 mpg123 -Z.

By '-n' option we can provide no. of arguments for a single invocation of command.

Even after I close the terminal where i wrote this command, the songs continue to play as the process mpg123 becomes an orphan and continues to run.

devikasingh@Interest:~$ ps -e | grep mpg123

                   7239 ?        00:00:01 mpg123

ps -f 7239

UID PID PPID C STIME TTY STAT TIME CMD

1000 7239 1 0 15:21 ? S 0:01 mpg123 -Z /media/MUSIC & PIC/audio_for_you/For You.mp3 /media/MUSIC & PIC/audio_for_you/In My Place.mp3 /

TwiggedToday
Is this a comment, an additional problem, or an answer?
ephemient
Glad you got it working alright, but without the random sort, won't it always play the same groups of songs? I would expect it would pick randomly from songs 1-10, then randomly from songs 11-20, etc. and these groupings would remain constant (until you add or remove files).
Matthew Flaschen
I did try it, the groupings are not constant. I get to hear different songs each time I type this command. But yes.. I get to hear the songs from the same folder all the time! It seems that while searching for '.mp3', it looks into the folders in some order..Each time the same folder is getting picked. Although the songs chosen from that folder are random.
TwiggedToday
That's basically what I meant. It looks into the folders in a deterministic order. You might want to try the random sort.
Matthew Flaschen
A: 

In both zsh and bash 4.0,

mpg123 -Z **/*.mp3

(Bash users will probably need to shopt -s globstar first.)

ephemient
I am a bash user. Kindly explain more about shopt -s globastar. What is it?
TwiggedToday
In Bash 4.0, it gives you ** which will perform a recursive search through the directory tree. Earlier versions of Bash do not support this feature.
ephemient