I'd like to generate a M3U playlist for a directory containing mp3 files on my server from the terminal. Since I'd like to ensure that every player will be able to stream those files I'd like to prefix each file entry with the absolute URL to that directory, like this:


So unfortunately simply doing ls -1 *.mp3 > play.m3u isn't enough. Is there a one-liner to achieve this?

5 Answers 5


This is @chronitis answer with some improvements :

  • stores the file name on the variable $playlist for later use
  • will delete the file if exists previously
  • writes the full path of the file on the playlist

The command

playlist='play.m3u' ; if [ -f $playlist ]; then rm $playlist ; fi ; for f in *.mp3; do echo "$(pwd)/$f" >> "$playlist"; done

To play it with mplayer on the command line also

mplayer -playlist play.m3u
  • I don't know why somebody downvoted this answer. This is the only solution that seems to work right out of the box and should be accepted or at least promoted. Thank you very much! Feb 7, 2015 at 17:02
  • what if i want to add multiple file formats into this command? Apr 8, 2017 at 4:58

I think the following one-liner should work:

for f in *.mp3; do echo "http://..../$f" >> play.m3u; done


You originally asked to create each entry as a web URL formatted line. In addition to replacing the local path with http://..., you'll also need to replace spaces with '%20'. So, long line, but here you go:

find /path/to/mp3s/ -name "*.mp3" | sed 's/ /%20/g' | sed 's|/path/to/mp3s/|http://www.server.com/serverpath/|g' > playlist.m3u
  • good idea! using a plus (+) sign instead would result in a more readable URL though
    – 3k-
    Sep 28, 2016 at 8:44

This bash script can do the job:

rawurlencode() {
  local string="${1}"
  local strlen=${#string}
  local encoded=""
  local pos c o

  for (( pos=0 ; pos<strlen ; pos++ )); do
     case "$c" in
        [-_.~a-zA-Z0-9] ) o="${c}" ;;
        * )               printf -v o '%%%02x' "'$c"
  echo "${encoded}"

rm -rf p.m3u
for f in *.mkv; do echo "#EXTINF:-1,SR:$f"$(rawurlencode $f) >> p.m3u; 
sed -i '1s/^/#EXTM3U\n/' p.m3u
rm -rf l.m3u
for f in *.mkv; do echo "#EXTINF:-1,SR:$f$f" >> l.m3u; 
sed -i '1s/^/#EXTM3U\n/' l.m3u

A little more developed version. The URL is encoded in proper .m3u style.


Like this post on askubuntu, I try mkpl with some examples proposed by creator:

  1. Create a playlist for one music album:

    cd myalbum
    mkpl myalbum.m3u
  2. Create a playlist of a film saga

    mkpl -d HarryPotter -f mkv HP_saga.m3u
  3. Create a shuffled playlist with my music collection

    mkpl -d "my_mp3_collection" "my_mp4_collection" -rs "my music.m3u"
  4. Create a shuffled playlist with my music collection and exclude dirs

    mkpl -d "my_mp3_collection" "my_mp4_collection" -r -s -e "my_mp3_collection/metallica" "my_mp3_collection/dk" "my music.m3u"
  5. Create a TV series playlist with max 15 tracks

    mkpl -d "my_series/GOT" -m 15 "got_first_15.m3u"
  6. Add into my music playlist new songs and don't add same file

    mkpl -d "new_collection" -rsu "my music.m3u" -a
  7. Create playlist with music and video files if files is greater then 10MB

    mkpl -d "my_files" -r -z 10485760 "multimedia.m3u"
  8. Create playlist with only number one and two tracks wit regular expression

    mkpl -d "my_mp3_collection" -r -p "^[12]|[012]{2}" "my music.m3u"
  9. Create a playlist for one music album and set the title:

    cd myalbum
    mkpl myalbum.m3u -t "My Album"
  10. Create a playlist and add UTF-8 encoding

    mkpl -d "new_collection" -r "my music.m3u" -g "UTF-8"
  11. Create a playlist and set image

    mkpl -d "new_collection" -r "my music.m3u" -I "new_collection/playlist_cover.jpg"
  12. Create a playlist and add remote file links

    mkpl -d "new_collection" -r "my music.m3u" -l,
  13. Create a playlist and set Windows backslash () folder separator (for Windows OS)

    mkpl -d "new_collection" -r "my music.m3u" -w

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.