Sometimes there is a need to physically sort the files on USB mounted media such as flash drives and SD cards. Not for viewing on the Mac while using Finder, but while managing the content of the USB drive from Mac OSX for use in other devices. Specifically if those other devices don’t have the ability to do any sorting.
While you can get the files in the order you want by copying them in that order (hint don’t rely on drag and drop, or copy and paste for proper ordering), i becomes very tedious if there are many files and more so if there are directories as well. I stumbled upon a neat open source utility called FatSort, available from SourceForge, that will do the job for you. Here is how to do it using Mac OSX.
Get The Software
Visit the FatSort SourceForge site and download the latest source. Yes, you have to build it, which means you will also at least have to have the Xcode command line tools installed (available from Apple for free), if not the full Xcode application. URL:
http://fatsort.sourceforge.net
Compiling
- Extract the source, open Terminal and change directory to the source location.
- Build the software by running the make command. Toward the end you will get an error related to the help, but the program is built:
macmini:fatsort-1.3.365 me$ make /Applications/Xcode.app/Contents/Developer/usr/bin/make -C src/ gcc -Wall -Wextra -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -c fatsort.c -o fatsort.o gcc -Wall -Wextra -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -c FAT_fs.c -o FAT_fs.o gcc -Wall -Wextra -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -c fileio.c -o fileio.o gcc -Wall -Wextra -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -c endianness.c -o endianness.o gcc -Wall -Wextra -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -c signal.c -o signal.o gcc -Wall -Wextra -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -c entrylist.c -o entrylist.o gcc -Wall -Wextra -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -c errors.c -o errors.o gcc -Wall -Wextra -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -c options.c -o options.o gcc -Wall -Wextra -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -c clusterchain.c -o clusterchain.o gcc -Wall -Wextra -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -c sort.c -o sort.o gcc -Wall -Wextra -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -c misc.c -o misc.o gcc -Wall -Wextra -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -c natstrcmp.c -o natstrcmp.o gcc -Wall -Wextra -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -c stringlist.c -o stringlist.o gcc -s -liconv fatsort.o FAT_fs.o fileio.o endianness.o signal.o entrylist.o errors.o options.o clusterchain.o sort.o misc.o natstrcmp.o stringlist.o -o fatsort ld: warning: option -s is obsolete and being ignored /Applications/Xcode.app/Contents/Developer/usr/bin/make -C man/ help2man --name "FAT sorting tool" ../src/fatsort -o fatsort.1 -N /bin/sh: help2man: command not found make[1]: *** [fatsort.1] Error 127 make: *** [man] Error 2
- Verify the program built successfully:
macmini:fatsort-1.3.365 me$ ls -al src/fatsort -rwxr-xr-x 1 me staff 61304 Apr 16 15:16 src/fatsort
- Now copy the built program to the location you want to run it from (I’ll use the user home directory: ~).
cp src/fatsort ~
Usage
- Mount the USB drive or SD card via normal means.
- Open Terminal.
- Run the mount command to find out which device its mounted on. The device shows up in the first column of the mount output. In the example below the USB drive is called HARMONY mounted on directory “/Volumes/HARMONY”, and the device is “/dev/disk2s1”:
macmini:~ me$ mount /dev/disk0s2 on / (hfs, local, journaled) devfs on /dev (devfs, local, nobrowse) map -hosts on /net (autofs, nosuid, automounted, nobrowse) map auto_home on /home (autofs, automounted, nobrowse) /dev/disk2s1 on /Volumes/HARMONY (msdos, local, nodev, nosuid, read-only, noowners)
- You now need root / administrative authority, so su to the root account. You will be prompted to enter your password, not the root user password.
macmini:~ me$ sudo su -
- Un-mount the media from Finder using the unmount command:
macmini:~ root# umount /Volumes/HARMONY
- Run the fatsort program from the location you copied it to. It has several options which can be obtained by running it with the “-h” parameter. The default is to sort directories first, then files, which is what I want so no options are needed. You must specify the device you identified above (in this example “/dev/disk2s1”).
macmini:~ root# ./fatsort /dev/disk2s1
That’s it! Unplug the media and put it in the other “dumb” device. The directories and files should be sorted alphabetically.