Sorting a Directory in the FAT File System

The venerable fat16 and fat32 file systems are still in widespread use today. Devices such as digital cameras, satellite navigation systems, memory sticks and mp3 players all make use of FAT.

The files in a FAT file system are arranged in a strict order. This can affect the way that some devices behave. For example, some MP3 players will play songs only in the order in which they are arranged on the device, rather than the more convenient alphabetical or alphanumeric order. Music players that use USB memory sticks and in-car USB systems can be affected in the same way. The only way to get the songs to play in a more sensible order is to sort the directory (folder) in which they are located.

One approach would be to create a new folder, move all of the files into it in the order you want them to play, and then delete the old folder. The new folder could then be renamed to the old folder name. If your mp3 player or USB stick contains more than just a few songs though, this won’t be practical. If you have access to a Linux system, a script or perl program could be written for the job. But there’s no need. A program called fatsort already does the job perfectly.

On the Linux system (Fedora here), install fatsort:

yum install fatsort

Attach your USB stick or mp3 player to the computer. If it mounts, unmount it, because fatsort works only on unmounted file systems:

umount /dev/sdb1

Now run the program:

fatsort -c /dev/sdb1

which should complete in a minute or so. All of the folders on the device will be sorted into alphanumeric order. The “-c” indicates case insensitivity. Remove the USB stick or MP3 player. You should find that all songs now play in the expected order. It worked well with my in-car system, which will play songs from a USB stick, and now does so in a sensible order.

Programs like fatsort are also available for Windows.

Unix file systems, eg Solaris ufs or Linux ext4, have an physical order for files too. That is to say, the file names occur in a given order within the directory object. The arrangement order can be seen with the ls -f command. It is not the order of the inode numbers, though it can be similar in recently built systems, as can be seen with the command ls -1fi, the “i” adding inode numbers and the “1” restricting output to one column.

6 thoughts on “Sorting a Directory in the FAT File System

  1. I really appreciate this. Nice, quick, Linux-compatible way to get a FAT-based mp3 player to play songs in alphanumeric order instead of directory order. Just what I needed.

    • Hi Dermot. And thanks for the first comment on this article in 7 years! Glad to hear it worked for you. I still use the above procedure to sort music on my USB sticks for the car and Sandisk MP3 players. Cheers, Jim.

  2. I came across this after having problems getting my 2018 Toyota Camry to play my audio books in order from a USB stick. In a book, Chapter 4 absolutely must not come before Chapter 2. (Their player is so ten-years-ago, and probably the biggest disappointment in my car.)

    I sorted all the folders by hand, by making copies and moving the files one at a time in the Linux command shell. It didn’t occur to me until later to look for a sorting utility, but this will be just the ticket for future music.

    • Hi Donb. Glad this worked for you, in a 2018 Toyota Camry. A 2010 Nissan of mine had the same sorting issue. My 2017 Ford, on the other hand (“Sync 3” audio) seems to sort files alphanumerically, without the need for the above procedure.

      I just tested a Roberts DAB radio (Stream 94i). It plays songs from an attached USB stick, again formatted with VFAT. It definitely sorts the files properly, without the need for a fatsort.

      Cheers, Jim.

  3. Hi Jim,
    I just came into your article trying to solve the exact same issue. It is incredible that I had been 7 years in the ignorance 😀
    Thanks a lot for the article!
    Cheers, Javier

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.