Abstract: Mac Users usually don't use suffixes such as .doc - after all, we've got resource forks! But what to do, if such a file (or thousands!) ends up on a *NIX machine (such as OS X Server!) without that ressource fork? Once again, shell scripting to the resuce!
A client of mine had some rather large amount of project-related files sitting on our Mac OS X Server -- dating back until 1997. Since nobody really needed them regularily, it had been decided to archive them. That's when somebody decided to actually have a look at those dust-covered files and found out that the majority of them were "broken" -- a catastrophe!
Well, as experience shows, in 99% of the cases when end-users claim that a file is "broken" it simply means that it won't open when they double-click it, so I wasn't too alarmed ;-). And indeed, it turns out that these older files had been served on a MacOS 8.x/9.x machine (using FileSharing and, I might add, years before I started working for this company) serving samesuch clients -- ergo none of these files had a suffix such as .doc or .jpg etc. And for some odd reason they had lost all of their ressource forks.
However, random samples showed that most of the files were okay, if you just guessed or remembered the filetype and appended the correct suffix so all that remained to be done is to write a script that performs this task automatically on every file. It's fairly simple, but having hardly any shellscripting experience it took me well over three hours to get it working (and it's still very crude!). Just in case any *NIX admin reading this hint is confronted with a bunch of suffix and resource-forkless Mac files, here's how to save yourself some time...
As often in UNIX, the first thing we do is break the problem down into components. The first is: Find all files that do not have a suffix. This, of course can be achieved with built-in tools, namely the find command. The next bit is to analyze each of these files using the file command and rename it accordingly. This step is handled in a script of its own called mtype2suffix.sh, so we get the following snippet:
find -type f -false -name *.* -exec mtype2suffix.sh {} ;
The mtype2suffix.sh script itself looks like this:
#!/bin/sh
mtype=`file -ib "$1";`
case $mtype in
audio/mpeg)
suffix=".mp3"
;;
application/msword)
suffix=".doc"
;;
# insert more mime-types as needed
*)
exit
;;
esac
mv "$1" "$1$suffix";
Important: the *) 'catch-all' statement with the following exit statement basically means "if you don't know what the file is, then leave it alone!," which seems more appropriate, than, say appending an emtpy suffix and thus messing with the file's modification date.
Mac OS X Hints
http://hints.macworld.com/article.php?story=200403140651023