Bash one-liner: Play a random .mp3 or .m4a file
1 min read
Here's the one-liner:
find /data/Music -type f \( -iname "*.mp3" -o -iname "*.m4a" \) -print | shuf -n 1 | xargs --delimiter '\n' vlc --play-and-exit
But rather than type all that every time we want to listen to music, let's put it in a script file, or rather three script files, and add a repeat feature:
"$1" -type f \( -iname "*.mp3" -o -iname "*.m4a" \) -printfind
source PrintAudioFiles "$1" | shuf -n 1 | xargs --delimiter '\n' vlc --play-and-exit
while : do source Shuffle1 "$1" done
Each of these three scripts can be executed independently.
To shuffle music on repeat, we simply run
./ShuffleForever /data/Music, where
/data/Music is the path to your music library.
To search, we can combine the
PrintAudioFiles script with the
grep command, e.g.
./PrintAudioFiles /data/Music | grep -i "modest mouse"
To play something from the search results, we can simply copy/paste the filename and run
vlc, for example:
vlc "/data/Music/Modest Mouse/Lonesome Crowded West/03 Convenient Parking.mp3" (don't forget the double quotes)