Skip to content

Instantly share code, notes, and snippets.

@DaWe35
Last active October 23, 2025 11:14
Show Gist options
  • Select an option

  • Save DaWe35/e96220ca6e8b86025506d3b27e29b556 to your computer and use it in GitHub Desktop.

Select an option

Save DaWe35/e96220ca6e8b86025506d3b27e29b556 to your computer and use it in GitHub Desktop.
Play 100 random songs on Linux from the selected folders with VLC media player
#!/bin/bash
# Directories containing your music files
MUSIC_DIRS=(
"/home/YOUR/MUSIC/FOLDER/"
"/home/YOUR/MUSIC/FOLDER/2/"
)
# Number of random songs to play
NUM_SONGS=100
# Check if VLC is installed
if ! command -v vlc &> /dev/null
then
echo "VLC is not installed. Please install VLC to use this script."
exit 1
fi
# Check if the directories exist
for MUSIC_DIR in "${MUSIC_DIRS[@]}"; do
if [ ! -d "$MUSIC_DIR" ]; then
echo "Directory $MUSIC_DIR does not exist."
exit 1
fi
done
# Find all music files in all directories (excluding subdirectories) and select random ones
FIND_COMMANDS=()
for MUSIC_DIR in "${MUSIC_DIRS[@]}"; do
FIND_COMMANDS+=("$MUSIC_DIR")
done
mapfile -t MUSIC_FILES < <(find "${FIND_COMMANDS[@]}" -maxdepth 1 -type f \( -iname "*.mp3" -o -iname "*.wav" -o -iname "*.flac" -o -iname "*.ogg" -o -iname "*.opus" -o -iname "*.m4a" \) | shuf -n $NUM_SONGS)
echo $MUSIC_FILES
# Check if enough files were found
if [ ${#MUSIC_FILES[@]} -lt $NUM_SONGS ]; then
echo "Not enough music files found in the directory."
exit 1
fi
# Play the random songs with VLC
vlc "${MUSIC_FILES[@]}" --play-and-exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment