#!/bin/bash ############################ ##### SET YOUR OPTIONS ##### ############################ # The chance of any given file to be selected is 1 in $CHANCE CHANCE=100 # The location of your music MUSICDIR=/cygdrive/d/media/mp3 # The list of file extensions to include MUSICXTNS=( MP3 FLAC OGG M4A ) ###################################### ##### OKAY WE'RE DOING STUFF NOW ##### ###################################### mscan() { shopt -s dotglob # this makes globs grab files beginning with . shopt -s nocaseglob # this makes globs case-insensitive local DIRSTACK[0]=$1 # create a local directory stack while [ ${#DIRSTACK[*]} -gt 0 ]; do local WDIR=${DIRSTACK[${#DIRSTACK[*]}-1]} # grab whatever directory is on top of the stack unset DIRSTACK[${#DIRSTACK[*]}-1] # and remove it from the stack for DIR in "$WDIR"*/; do # this matches only directories if [ -x "$DIR" ]; then # if the directory is +x we can read it DIRSTACK[${#DIRSTACK[*]}]=$DIR # so put it into the stack fi done for EXT in ${MUSICXTNS[*]}; do # go through the list of extensions for FILE in "$WDIR"*.$EXT; do # go through the files matching this extension (works because of nocaseglob) if [ ! -f "$FILE" ] || [ -d "$FILE" ]; then # if this file doesn't exist, we probably got no matching files and had the glob returned; also make sure this isn't a directory continue # if either of those bad things happened, skip the rest fi if [ -r "$FILE" ] && (( $RANDOM % $CHANCE == 0 )); then # if the file is readable and passes the random check echo $FILE # add it to the playlist fi done done done } mscan $MUSICDIR | sort -R # pipe the whole thing through sort -R to make it more random