Automatically create .m3u playlists with bash (for QNAP iTunes Server)

One of the cool features of my new QNAP 412 Turbo Nas is iTunes Server. This basicly allows you to create playlists, and share them among your home network. I haven’t figured out yet if it’s also possible to sync these to your iOS devices, but we can atleast (easily) share our music library to all our devices.

Qnap comes with so called ‘smart playlist’, but I don’t like those. I prefer my playlists per CD or folder. In order for this to work we’ll have to create an m3u playlist for EACH folder you want to share, this can be a daunting task.. Here’s a way to automate this. This script uses recursion to find all audio files in the parent folder + child folders. Since iTunes uses the m3u’s filename as playlist title, this script will use the folder name for this.

SSH to your server (QNAP default user = admin), and cd yourself to your music folder. Now create a bash script and give it execute permission:

cd /Qmultimedia/Music/
chmod +x

Now enter the following code. I found this neat script here, and stripped it down to the necessary commands, and implemented the removal of old m3u’s (in case you delete/update Music folders).



/opt/bin/find . -type f -name "*.m3u" -exec rm -f {} \; # remove all existing m3u's

rm -f $M3Ulist

indexCurrDir ()
FileList="" # initialize empty variable FileList
for FileTypes in "ogg" "mp3" "flac" "wav" # loop over file types.
FindFiles=$(/opt/bin/find $(pwd) -type f -iname "*.$FileTypes" | sort)

if [ "${#FileList}" != "0" ] # do not write m3u file if file list is empty
echo "$CurrDir"
m3uName=$(basename $CurrDir)

echo "Writing m3u playlist."
echo "$FileList" > "${m3uName}.m3u"
echo "$CurrDir/${m3uName}.m3u" >> "$M3Ulist"

AllDirs=$(/opt/bin/find $(pwd) -type d | sort)

for Directory in $AllDirs
cd "$Directory"

exit 0

Now you can easily create m3u’s for your complete music folder by running:


You can also put this in a cronjob for automating things. Here’s more info on cronjobs on a QNAP.