Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Salvaging MacOS files in a UNIX World UNIX
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.

With lots of help from Carsten and Cryx, thanks! Originally posted here.
    •    
  • Currently 1.25 / 5
  You rated: 2 / 5 (4 votes cast)
 
[8,630 views]  

Salvaging MacOS files in a UNIX World | 9 comments | Create New Account
Click here to return to the 'Salvaging MacOS files in a UNIX World' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Salvaging MacOS files in a UNIX World
Authored by: Spartacus on Mar 19, '04 11:34:38AM

The resource fork has nothing to do with file type and creator. File type and creator are metadata and the resource fork is just a second place where to store data, in an organised way.



[ Reply to This | # ]
Salvaging MacOS files in a UNIX World
Authored by: etrepum on Mar 19, '04 11:52:18AM

That is entirely true, however, cross-platform file formats (as in the examples, mpeg and msword) can generally be "recovered" in this simple fashion, as they don't really use the resource fork for anything but document associations and icons.



[ Reply to This | # ]
Salvaging MacOS files in a UNIX World
Authored by: foilpan on Mar 19, '04 12:26:01PM

this hint will work as long as the files don't store data in the resource forks, like some old style apps did. those files will probably be toast.



[ Reply to This | # ]
To -b or not to -b
Authored by: the1truestripes on Mar 19, '04 01:48:30PM

FYI the -b makes file only recognize about 550 different file types while the lack of the -b lets it recognize about 8000 types. Unfortunately the output without -b is harder to parse (more like free form text then MIME types, which is fair as file predates MIME types by 15+ years).

If 'file -b' scratches it's head over a lot of your files, give it a shot on the "unknown" files without the -b.

[ Reply to This | # ]

To -i or not to -i
Authored by: seanasy on Mar 19, '04 02:01:05PM

From the file man page:

-b Do not prepend filenames to output lines (brief mode).

I think you meant the -i option:

-i Causes the file command to output mime type strings rather than the more traditional human readable ones. Thus it may say ``text/plain; charset=us-ascii'' rather than ``ASCII text''. In order for this option to work, file changes the way it han- dles files recognised by the command itself (such as many of the text file types, directories etc), and makes use of an alternative ``magic'' file. (See ``FILES'' section, below).

[ Reply to This | # ]
To -i or not to -i
Authored by: tomholio on Mar 20, '04 06:08:50AM

Thanks (to both of you) for pointing this out! I must admit, that I was sloppy when reading the manpage and didnt't notice the bit about 'using another magic file' and simply thought the sparse output is easier to match in the case statement.



[ Reply to This | # ]
newer version
Authored by: sjk on Mar 19, '04 03:35:23PM
On Panther:
% file --version
file-4.02
magic file from /usr/share/file/magic
Source distribution of the latest version (currently 4.07) can be found here.

[ Reply to This | # ]
How to Salvage File with Missing Resource Fork?
Authored by: samdiener on Apr 02, '04 02:21:24AM

I transfered a file from an os 9 machine to an os 10.3.2 machine using a flash memory key. Apparently, in the transfer the resource fork on the file was lost. The application (turbotax) can't open the file, and they are telling me it's an operating system issue, not an application issue.

The type and creator code also were missing. I went into classic and used resedit to fix the type and creator code. Now, the file has the Turbotax icon and all, but the program still can't open it, and resedit says the resource fork is missing.

Is there a way to create a new resource fork for the file so that I could open it again?

When I go to versiontracker, I see programs which promise to delete the resource fork me, but not ones to create or restore one. I didn't understand what to do after reading this thread so far.

Any help you could provide would be appreciated.
Thanks,
Sam



[ Reply to This | # ]
How to Salvage File with Missing Resource Fork?
Authored by: hamarkus on Apr 03, '04 05:34:47AM

If you still have the original files, you could try to zip or maybe better 'sit' them, using 'DropStuff' or 'DropZip' (you can get a free trial version, 7.x for OS 9, here: http://www.stuffit.com/cgi-bin/stuffit_loginpage.cgi?standardmac ), before transferring them.



[ Reply to This | # ]