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.
Riiight, unfortunately it didn’t recognized FAT32 with 64K cluster size.
check_bootsector: Cluster size is larger than 32k!
read_bootsector: This is not a FAT boot sector or sector is damaged!
openFileSystem: Failed to read boot sector!
sortFileSystem: Failed to open file system!
main: Failed to sort file system!
FAT and FAT32 are different.
That was a nice one-line reply, but it had nothing to do with my issue.
I reformatted the card to FAT32 32K cluster size back then and now it works perfectly.
File system: FAT32.
Sorting directory /
Sorting directory /.Spotlight-V100/
Sorting directory /.Spotlight-V100/Store-V1/
Sorting directory /.Spotlight-V100/Store-V2/
Sorting directory /.Spotlight-V100/Store-V2/E13D679A-C486-403F-BBDE-E456F6A83648/
Sorting directory /.Spotlight-V100/Store-V2/E13D679A-C486-403F-BBDE-E456F6A83648/journals.live/
Sorting directory /.Spotlight-V100/Store-V2/E13D679A-C486-403F-BBDE-E456F6A83648/journals.scan/
Sorting directory /.Trashes/
Sorting directory /.fseventsd/
Sorting directory /A-F/
Sorting directory /A-F/Adele/
…..
Sorry. I thought it had everything to do with your issue since the article is about sorting a FAT volume, not a FAT32 one.
Whoops, didn’t noticed that you’re talking about FAT16 (I assume FAT16 as FAT12 is too legacy?)
Right, I can assure the fatsort is a good stuff for FAT32, though limited to 32K cluster size. No biggie with no 64K support, anyway. I’ve been using DriveSort on Windows for several days and found out that it would corrupt my audio files occassionally, without any CRC errors! Stay away from DriveSort!
Hi, thanks for taking the time to write this.
I’m running into some issue though.
How exactly do you run fatsort from the location you copied it to while staying in root# ? I can’t access the directory.
And when I exit the root and try to run it from its location I get:
MBP-de-Thomas:~ Thomas$ ./fatsort /dev/disk3s1
openFileSystem: Permission denied!
sortFileSystem: Failed to open file system!
main: Failed to sort file system!
root should be able to access any file in any directory. You may need to alter the fatsort binary to have execute permissions with 755 (rwxr-xr-x) or 775 (rwxrwxr-x). It looks like from the error that its not the execute permission on the program, but rather the filesystem your trying to sort. Make sure the filesystem mounted is read/writable. Again, as root, it shouldn’t be an issue.
Well I managed to fix my problem by only entering sudo -s instead of the sudo su command. This gives me the permissions required and lets me access the right directory. I can now sort my car music properly.
Thanks for the answer anyway 😉
Hi — I’m a complete command-line ignoramus, trying to step through this, and thanks to your methodical explanation I feel tantalizingly close. But I’m still failing. Here’s the story:
I’m running Mac OS 10.15.5. I download the latest Fatsort, 1.6.2.605, from Sourceforge and it unpacks to my desktop. I open terminal and install Xcode using “xcode-select –install”, which seems to work fine, GUI and all. I navigate to the src folder in the Fatsort folder, then this is what happens (see below). What am I doing wrong? — My sincere thanks in advance for any advice, Larry
————————————
Larry-Gilmans-iMac-5:fatsort-1.6.2.605 larrygilman$ cd src
Larry-Gilmans-iMac-5:src larrygilman$ make
uname: illegal option — o
usage: uname [-amnprsv]
gcc -Wall -Wextra -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE -D __OSX__ -c fatsort.c -o fatsort.o
In file included from fatsort.c:38:
In file included from ./FAT_fs.h:78:
./deviceio.h:57:4: error: Unsupported OS!
#error Unsupported OS!
^
./deviceio.h:61:1: error: unknown type name ‘DEVICE’
DEVICE *device_open(const char *path);
^
./deviceio.h:64:24: error: unknown type name ‘DEVICE’
int64_t device_seekset(DEVICE *device, int64_t offset);
^
./deviceio.h:67:21: error: unknown type name ‘DEVICE’
int64_t device_read(DEVICE *device, void *data, uint64_t size, uint64_t n);
^
./deviceio.h:70:22: error: unknown type name ‘DEVICE’
int64_t device_write(DEVICE *device, const void *data, uint64_t size, uint64_t n);
^
./deviceio.h:73:17: error: unknown type name ‘DEVICE’
int device_sync(DEVICE *device);
^
./deviceio.h:76:18: error: unknown type name ‘DEVICE’
int device_close(DEVICE *device);
^
In file included from fatsort.c:38:
./FAT_fs.h:329:2: error: unknown type name ‘DEVICE’
DEVICE *device;
^
8 errors generated.
make: *** [fatsort.o] Error 1
Larry-Gilmans-iMac-5:src larrygilman$
Apologies for the extremely late reply. I’m not sure what’s happening. If you didn’t run a “make config” to prep the environment that could be the cause. It could be that the fatsort source is just too old to compile now without some tweaks due to deprecated items in the source. It’s been a long time since I wrote that original post, and no longer need fatsort. Sorry I am unable to further assist.
Hello,
I already ran succesfully fatsort on some other Volumes, but i have trouble now:
wcpifrima100m:~ root# diskutil unmount /dev/disk2s1
Volume NONAME on disk2s1 unmounted
wcpifrima100m:~ root# ./fatsort /dev/disk2s1
main: WARNING: The C locale does not support all multibyte characters!
device_open: Operation not permitted!
openFileSystem: Operation not permitted!
sortFileSystem: Failed to open file system!
main: Failed to sort file system!
It’s a large collection of music and there is maybe somewhere some japanese ideogram in file names, does it come frome that?
Thanks for your help