WSL bash vlc music random playback music player

Creating a Random Music Player with VLC in WSL Bash

Sharing a script I wrote for a music player-like program that randomly plays music with VLC in Windows 10 WSL Linux bash. I wrote it on a whim. It could probably work without VLC too. Well, I only use VLC anyway.

Shou Arisaka
6 min read
Oct 4, 2025

Sharing a script I wrote for a music player-like program that randomly plays music with VLC in Windows 10 WSL Linux bash.

I wrote it on a whim. It could probably work without VLC too. Well, I only use VLC anyway.

Tool to Randomly Play Music Files with VLC

This script is for randomly playing music files in a specified directory and works in the WSL (Windows Subsystem for Linux) Bash environment. Below, I’ll explain the main parts of the script and their operation step by step.

shufmusic(){

    : Play randomized music on current directory with vlc.exe. compatible with wsl bash.
    : e.g. shufmusic
    : e.g. shufmusic mp4

    # Create a list of music files and save to list.txt
    # List files with extensions wav, mp3, flac, wave
    [[ "${1}" =~ mp4|video ]] && find -L -regextype posix-extended -regex '.*\.(wav|mp3|flac|wave|mp4|avi)' > shufmusic_list.tmp.txt || find -L -regextype posix-extended -regex '.*\.(wav|mp3|flac|wave)' > shufmusic_list.tmp.txt

    # Confirm the list file is not empty
    [ -s shufmusic_list.tmp.txt ] || { printf "\n\n${red}E: The list file empty. It's seems something worng.${end}\nINFO: You may have to use ${cyn}shufmusic mp4${end}.\n\n" ; return 1 ; }

    printf "INFO: $(wc -l shufmusic_list.tmp.txt | awk '{print $1}') files found.\n"

    # Main loop
    while [ true ];
    do
        sleep 1

        # If the list file is not empty, randomly select a song
        [ -s shufmusic_list.tmp.txt ] && currentMusic="$(cat shufmusic_list.tmp.txt | shuf | head -n 1)" || { printf "\n\n${red}E: The list file is being empty.${end}\n\n" ; return 1 ; }

        # Confirm currentMusic is not empty
        [ -z "${currentMusic}" ] && { printf "\n\n${red}E: \$currentMusic is empty.${end}\n\n" ; return 1 ; }

        # Delete the selected song from the list
        perl -i -pe "BEGIN{undef $/;} s/$(printf "%q" "$currentMusic" | sed -Ee 's/\//\\\//g')\n//smg" shufmusic_list.tmp.txt

        # Play the song using vlc.exe
        vlc.exe "$(wslpath -w "$(realpath "${currentMusic}")")" &

        # Wait for a certain time according to the song length
        [[ "${currentMusic##*.}" =~ mp4|avi ]] && sleep "$(ffprobe -i "${currentMusic}" -show_entries format=duration -v quiet -of csv="p=0")" || sleep "$(sox --info -D "${currentMusic}")"

        sleep 1
    done

}

This script is for playing music files in a specified directory, explained in the following steps:

  1. Using the find command, generate a list of music files (.wav, .mp3, .flac, .wave) and save it to shufmusic_list.tmp.txt. This list is used for random playback. If $1 (the first argument passed to the script) is mp4 or video, mp4 and avi files are also added to the list.

  2. Check if the list file is not empty; if it is empty, output an error message and exit the script.

  3. The main loop begins, set up as an infinite loop.

  4. Inside the loop, there’s a 1-second break.

  5. If the list file is not empty, randomly select a song and store it in currentMusic.

  6. Confirm that currentMusic is not empty; if it is empty, output an error message and exit the script.

  7. Delete the filename contained in currentMusic from the list file and play that song.

  8. Depending on the song format, get the playback time with ffprobe (for mp4 or avi) or sox (for other formats) and wait for the corresponding duration.

  9. Finally, there’s a 1-second break before moving to the next song.

Installing Additional Packages

To run this Bash script, several external packages and tools are required. Here’s an explanation of those packages:

  1. vlc.exe:

    • VLC Media Player is a famous media player application used for playing music and video files. This script uses vlc.exe to play music files. vlc.exe is typically the Windows version of VLC Media Player. You need to install this application and add it to your PATH.
  2. find:

    • The find command is used to search for files in a specified directory. In this script, it’s used to find music files with specific extensions. Generally, you can install the find command using a Linux distribution (or package manager provided within WSL). For example, on Ubuntu, you can install it with a command like sudo apt-get install findutils.
  3. shuf:

    • The shuf command is used to randomly rearrange lines in a text file. This script uses shuf to randomly select songs from the music file list. shuf is generally included in Linux distributions and doesn’t require additional installation.
  4. wslpath:

    • The wslpath command is used to convert between Windows paths and Linux paths. This script uses wslpath to convert Windows paths to Linux paths to pass to vlc.exe. wslpath is a utility provided within WSL and is usually automatically available when you install WSL.
  5. realpath:

    • The realpath command is used to get the absolute path of a specified file. This script uses realpath to get the absolute path of currentMusic. realpath is generally included in Linux distributions and doesn’t require additional installation.
  6. ffprobe:

    • ffprobe is part of the FFmpeg project and is a command used to get properties and metadata information of video and audio files. This script uses ffprobe to get the playback time of mp4 or avi files. ffprobe is usually provided with FFmpeg. When you install FFmpeg, ffprobe becomes available.
  7. sox:

    • sox is a command-line tool for processing and manipulating audio files. This script uses sox to get the playback time of music files. sox can be installed through Linux distributions. For example, on Ubuntu, you can install it with a command like sudo apt-get install sox.

By installing these tools and packages and adding them to your PATH, you should be able to run this script normally. In particular, make sure vlc.exe, find, shuf, and wslpath are added to the PATH environment variable.

Summary

Even though I made it myself, it’s quite convenient. When you discover a good song, you just cd to the folder with the file and run this to randomly play it, so it’s perfect for the “I just want to play some music” demand. VLC also has a playlist function, but drag-and-drop is tedious, and FLAC is heavy. With this command, if you run it in a directory with symbolic links, you can easily randomly play even tens of thousands of high-resolution files. Nice.

I’ll upload code updates to github or .bashrc when there are updates.

Share this article

Shou Arisaka Oct 4, 2025

🔗 Copy Links