mpg123 "`locate -r '\.mp3$'|awk '{a[NR]=$0}END{print a['"$RANDOM"' % NR]}'`"

Random play a mp3 file

Pick a mp3 at random and play it. Assumes the availability of locate with an updated db and mpg123 Not the most useful command I guess, but all of the really useful ones are taken...

2009-02-23 13:53:12

These Might Interest You

  • Play files in shuffle mode with bash and mpg123. Why bother using big-as-hell stuff like mplayer? This will play all your music files contained in */* (in my case author/song.format) with bash and mplayer showing a nice output. Show Sample Output

    while [[ 1 ]]; do n=( */* ); s=${n[$(($RANDOM%${#n[@]}))]}; echo -e " - $s"; mpg123 -q "$s"; done
    XayOn · 2013-01-18 09:20:58 0

  • 0
    mplayer $(find . -iname '*.avi' | shuf -n1)
    fooacad · 2012-11-13 09:00:12 0
  • unsets variables used by the one-liner sets up the IFS bash variable to not be affected by whitespace and disables extra glob expansion uses read to slurp the results of the find command into an array selects an element of the array at random to be passed as an argument to mplayer

    unset files i; set -f; O=$IFS; while IFS= read -r -d $'\0' files[i++]; do :; done < <(find . -name '*.avi' -print0) && IFS=$O; set +f && echo "Running: mplayer \"${files[ $(( $RANDOM % ${#files[@]} )) ]}\""
    DEinspanjer · 2009-02-18 16:53:57 1
  • "play" is part of "SoX" SoX - Sound eXchange, the Swiss Army knife of audio manipulation. For details, see: man sox Show Sample Output

    play $audio_file
    mpb · 2009-03-17 11:30:02 2
  • Create a bunch of random files with random binary content. Basically dd dumps randomly from your hard disk to files random-file*. Show Sample Output

    no_of_files=10; counter=1; while [[ $counter -le $no_of_files ]]; do echo Creating file no $counter; dd bs=1024 count=$RANDOM skip=$RANDOM if=/dev/sda of=random-file.$counter; let "counter += 1"; done
    rajaseelan · 2009-07-31 16:34:47 2
  • This uses bash's Process Substitution. It is necessary to give the full path of the file, thus why the filenames are filtered through realpath. In theory, this command should also work with mplayer and mplayer2 (using -playlist instead of --playlist). Modifications: - You can search specific directories by inserting them between "find" and "-type". - Edit the parameter of "-iname" to change the type of file searched for. - Remove everything after "realpath" to play the files in order.

    mpv --playlist <(find -type f -iname '*.mp3' -print0 | xargs -0 realpath | sort -R)
    hololeap · 2017-09-28 23:28:40 0

What Others Think

madplay -z `locate -ir \.mp3$` plays .MP3 files too
maht · 481 weeks and 6 days ago
nice. can substitute program and file type to suit, sweet.
my0pic · 481 weeks and 5 days ago
zsh : mplayer -shuffle ./**/*.mp3
susannakaukinen · 480 weeks and 2 days ago
mplayer `find ./ -name '*.mp3' | shuffle | head -1`
jnash · 477 weeks and 3 days ago
switch '\.mp3$' for '\.mp3$|\.ogg$|\.m4[abrp]$' (etc) you could do "|| afplay ... " for formats mpg123 doesn't play.
jasonm23 · 249 weeks ago
go with $() over `` ($() can be nested.)
jasonm23 · 249 weeks ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: