Automatically delete Mac OS X dot files from MS-DOS filesystems

Feb 07, '11 07:30:00AM

Contributed by: lsloan

I often use a USB flash drive on my Mac that's formatted with an MS-DOS filesystem. I usually copy JPGs and MP3s onto it for use with my household DVD player. When I plug the flash drive into the DVD player's front-panel USB port, I can view the files on my TV. The problem is that the DVD player is very simple and it shows all the dot files that Mac OS X puts on it, like .Trashes and a .DS_Store in almost every folder.

I wanted to prevent Mac OS X from writing those files to MS-DOS filesystems at all, but I couldn't find a way to do that. My second thought was to write an AppleScript that would execute whenever the eject operation was requested, deleting the dot files, but I couldn't find a way to do that, either. So, being an old UNIX guy, I decided to try going down to that level and writing a wrapper script for umount, the program that does a lot of the work when ejecting (or 'unmounting') a filesystem.

This has worked out for me very well, but this solution might not be for everybody. This hint will explain how to install a umount wrapper script, but great care must be taken while doing so. You'll be using Terminal.app, working as the root user, and moving important OS utilities. A mistake in these steps could cause a lot of trouble for you.

To install the wrapper:

What this script will do is check that the filesystem being ejected is of type 'msdos.' If it is, it will look through all the folders on the filesystem and delete the ones that begin with a dot. (It purposely ignores "." and "..", of course). All output from the script is sent to the system logger, so if you run Console.app, you'll be able to see the output in the system.log. It will show the name of the filesystem being ejected, its type, and if files are being deleted, you'll see their names, too. After the checking and deleting is complete, the script calls the old umount-orig utility to eject the now-clean filesystem.

This may be obvious, but if you intentionally copy dot files (like your .bashrc file) to your MS-DOS filesystem, they will be deleted when you eject the filesystem. A couple possible ways to prevent this would be to put your dot files into a ZIP file or change this script to be more specific about which files it deletes.

In my opinion, Apple should put an option into the OS to not put its special dot files onto MS-DOS filesystems and the option should be enabled by default. Users that then need those dot files can then disable the option.

[crarko adds: I haven't tested this one. Please be careful with this one if you try it. There are a couple of earlier hints that cover similar territory as this one.

Note: Correction made to the script per the author to add the -fr command to the line:
find "$1" -depth -name '.[^.]*' -print -exec /bin/rm -fr {} \;.]

Comments (34)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20110204124029798