Sorting Directories on FAT File Systems

Sort files in FAT file system directories

I have a car stereo in which I can plug a USB stick and play music or audio book files. It handles MP3, OGG, FLAC, and so on, so it's great that way. Newer vehicles let you play music from your phone, but I have no music on my phone, so that's not something I use. However, the software on the stereo that reads the files off the USB stick does not sort the files. If the tracks are copied onto the USB stick in order 10, 3, 8, 1, 4, 9, 7, 5, 8, 6, 2, then that is the order they will be played (unless using shuffle mode, in which case the original order does not matter).

Note that this technique applies only to FAT (FAT-32, VFAT) file systems.

To sort the files, I copy the MP3 files I want into a directory on the USB stick, and then run this script:

#!/bin/bash
#
#  Sort files alphabetically in the current directory.

FILES=()

for FILE in *; do
    FILES+=("${FILE}")
done

TEMPDIR=$(mktemp -d work_XXXXXXX)

echo ${#FILES[@]} files
for FILE in "${FILES[@]}"; do
    mv "${FILE}" "${TEMPDIR}"
done

for FILE in "${TEMPDIR}"/*; do
    mv "${FILE}" .
done


rmdir ${TEMPDIR}

I have this script, named sort-dir, in my PATH, so I type:

cd /media/usb/ARTIST/ALBUM
sort-dir

and now that directory is sorted and plays in-order on my car stereo.

Back when I wrote this script, I looked around for FAT file system directory sorters, and there a variety out there, some commercial offerings, but this tool does the job simply, quickly, and reliably.


Receive Updates

ATOM

Contacts