FAT file system sorting script for MP3 players, etc.

Many MP3 players, OGG players, DVD/DIVX/XVID players, etc. use FAT file system and do not sort directory entries, but use the natural sorting in the file system, which typically depends on the order the files have been copied to the device. It is really annoying to not been able to listen to compressed music in the right order or having the chapters of your favorite series unordered.

There are several solutions for it:

  • fatsort: works directly on the fat filesystem device. Very efficient, but you need to umount the filesystem and be sure it is not corrupted. Not adequate for the “regular” user.
  • Another fatsort: this is for Windows, who cares…
  • Scripts people have done: they work by moving files around in the right order. The work for FAT, but not necessarily for other file system. There is a nice such scripts in linuxforums called findalpha.

I have written my own script you can find below. It started as an enhancement to the one on linuxforums, but I have finally rewritten it completely. You can put several paths in the command line or none to work on the current working directory. You can use it as a nautilus script (as I do). Just put the script on ~/.gnome2/nautilus-scripts and you will be able to sort a whole folder just by right-clicking on it and selecting the script.

I am sorry this blog service does not allow to attach text files, so you will have to copy the script to a text file, put a cool name like “sortdirfat” and save to a place in the path like $HOME/bin, /usr/local/bin or right to ~/.gnome2/nautilus-scripts. Do not forget to give exec permission to the script.


-------------------- CUT BELOW THIS LINE -------------------------

# This script is in the public domain
# Usage:
#   sortdirfat [<paths>]

# Reorder recursively the directory entries alphabetically in FAT filesystems
# (maybe in other FS too, who knows).  Useful for preparing files for playing on
# an MP3 player that follows natural directory order.
# Action takes place in directories given as argument or in current directory
# if no arguments are given.
# It can be used as a nautilus script: just copy or symlink to
# ~/.gnome2/nautilus-scripts/
# It also works on files and directories containing spaces and maybe other
# special characters.


if [ $# -eq 0 ]; then
  set .

find "$@" -type d |
while read dir; do
  cd "$dir"
  tmp=`mktemp -d -p .`
  ls -UA | grep -v `basename $tmp` | xargs -n1 -d"\n" -I'{}' mv '{}' $tmp
  cd $tmp
  ls -A --group-directories-first | xargs -n1 -d"\n" -I'{}' mv '{}' ..
  cd ..
  rmdir $tmp
  cd "$start"
-------------------- CUT ABOVE THIS LINE -------------------------


Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión /  Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión /  Cambiar )


Conectando a %s