Use find, cat, etc… to generate an M3U playlist from album playlists

I have a music folder with selected playlists in each folder titled "playlist.m3u" (should also match *.m3u8).

What I would like to do is have a shell script generate a playlist concatenated with all these selected tracks. But in order for the playlist to work properly, the folder name has to be prepended before the song title.

Here’s an example directory structure:

Main Folder
 `-> Album 1/
 `-> Album 2/
 `-> Album 3/

The resulting main-playlist.m3u file should look something like this:

Album 1/01. Song1.flac
Album 1/02. Song2.flac
Album 1/03. Song3.flac
Album 1/04. Song4.flac
Album 2/1-01. Song1 (Disc 1).flac
Album 2/1-02. Song2 (Disc 1).flac
Album 2/2-01. Song1 (Disc 2).flac
Album 2/2-02. Song2 (Disc 2).flac
Album 3/001. Song 001.mp3
Album 3/002. Song 002.mp3
Album 3/003. Song 002.mp3
Album 3/100. Song 100.mp3
Album 3/101. Song 101.mp3

What is the best and most efficient way for doing this for a song library with, eventually, thousands of tracks?

Asked By: mrjpaxton


Okay, I’ll just post a way I was able to figure this out.


for album in */; do
    while read -r track; do
        printf "%s%sn" "$album" "$track"
    done < "${album}/playlist.m3u"
done > "main-playlist.m3u"
Answered By: mrjpaxton

Use find? e.g.:

find . -type f -name '*.flac' -or -name '*.mp3'

Or with absolute paths:

find $PWD -type f -name '*.flac' -or -name '*.mp3'

Case insensitive:

find $PWD -type f -iname '*.flac' -or -iname '*.mp3'

If you really want to extract the list from these "m3u" files, something like this would work:

find . -name '*.m3u' -print0 |
while IFS= read -r -d '' plist; do
  len=$(wc -l < "$plist")
  dir=$(dirname "$plist")
  paste -d/ <(yes "$dir" | head -n$len) "$plist"
Answered By: Thor
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.