Mac OS X Resource Fork and Command Line Tips

, ,

This page is tips about Mac OS X's command line tools about Mac's resource fork.

The info on this page is tested on Mac OS X 10.4.x and OS X 10.6.x.

Note: since Mac OS X 10.7 (released in 2011), the old syntax for accessing resource fork no longer works. The new syntax is ls -l filename/..namedfork/rsrc.

Resource Fork

What's a resource fork?

Resource fork is a feature of the Mac's file system HFS Plus, and older file systems used by Mac such as HFS and “Macintosh file system”.

A file has 2 parts: ① data fork, ② resource fork. The data fork is the file's main content, the resource fork stores some extra data such as icon, version number, embedded images, image thumbnail, and much more.

Some app store main data in resource fork. For example, Quick Time movie files used to store main movie data in the resource fork. This means, you should not simply remove resource fork data.

Is resource fork still used?

Yes. The use of resource fork is discouraged for normal files. However, program executable files such as those “.app” may still need the resource fork intact to work, and many programs, including Apple's software, still create resource fork as of .

for example, see: Safari “.webloc” File.

How to use the command line to find out if a file has resource fork?

Use the ls -l command with /rsrc suffixed to the file name.

Note: since Mac OS X 10.7 (released in 2011), you have to use /..namedfork/rsrc.

For example, ls -l file.png/rsrc. If the reported file size is 0, then it has no resource fork. Here's a command line log as a example:

◆ ls -l file.png
-rwxrwxrwx    1 xah      staff     3184339 Jun 26  2001 file.png

◆ ls -l file.png/rsrc
-rwxrwxrwx    1 xah      staff       87410 Jun 26  2001 file.png/rsrc

The file size reported by the unix command “ls”, does it count resource fork?

No. (but if you append your file name with “/rsrc”, then “ls” will report the resource fork's file size.)

How to delete a file's resource fork using command line?

You can overwrite it with a empty file. Suppose you want to delete resource fork in “file.png”. Do like this:

cp /dev/null file.jpg/rsrc

See also: Perl Script for Removing Mac Resource Fork.

How to delete a file properly (including resource fork) using the command line?

Using /bin/rm is probably doing the right thing. To be safe, one can delete the resource fork first. Example:

cp /dev/null myhouse.jpg/rsrc && rm myhouse.jpg

When using “get info” in Finder, it reports file size by two numbers, ⁖ “592KB on disk (601,189 bytes)”. What's these two figures exactly? and does KB mean 1024 bytes or 1000 bytes? Does either count the resource fork?

The one in parenthesis is the actual file size (counting the resource fork). The one not in paren is the disk space it occupies, and the M and K abbreviations are multiples of 1024.

FileFinderls
alice.jpg (has resource fork)584 KB on disk, (595,041 bytes)484995
alice2.jpg (no resource fork)476 KB on disk, (484,995 bytes)484995

Note: starting with OS X 10.6, k may mean 1000, not 1024. See: Computing: mega vs mibi, kilo vs kibi.

Does “cp” command copy resource fork?

Yes, as of Mac OS X 10.4

How to view resource?

Look at /usr/bin/DeRez. Type “man DeRez” to read how to use it.

In 10.4, resource fork related tools are available on the Developers tools CD that came with Mac OS X at least 10.1 up to 10.4. When installed, it's at /Developer/Tools/.

File Type/Creator

File Type and Creator

What's a File Type/Creator code?

In Windows, file names has a suffix , ⁖ {.txt, .jpg, .bat, .dll}. This suffix indicates the file's type, and what application is used when user tries to open them. (See: filename extension)

In Mac Classic, the file type and the application associated with the file is stored with the file in the file system. This is the file type and creator code. (See: Type code and Creator code. )

In Mac OS X, the use of file type and creator code is discouraged and is no longer supported starting with Mac OS X 10.6 “Snow Leopard” (2009).

Is file type and creator stored in resource fork?

No. File {type, creator} info is stored in the metadata that {HFS, HFS+} file systems support, along with lock/unlock status, the flag that says whether there's a custom icon, date/time created/modified, etc.

The file type/creator info, and any resource fork, are both lost when transferring the file to other OS without care. When using unix command line utils (cp, tar, gz, …), whether these data are kept depends on which command you are using. Also, each version of OS X, Apple updates some command line tools so that they preserve these data.

How to find out a file's type and creator code using the command line?

Use “GetFileInfo”. Example:

GetFileInfo -c file.jpg
GetFileInfo -t file.jpg

The “-c” is for creator code, the “-t” is for file type code.

In 10.6, the command “GetFileInfo” is at /usr/bin/.

http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man1/GetFileInfo.1.html

How to change a file's type and creator code using the command line?

Use the command “SetFile”. Like this:

/usr/bin/SetFile -type 'GSPb' -creator 'GSP+' myFile.gsp

http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man1/SetFile.1.html

If you want to set it to a bunch of files in dir, you can do it like this:

find . -name "*\.gsp" -print0 | xargs -0 -l -i SetFile -type 'GSPb' -creator 'GSP+' "{}"

In the above example, all file in the current dir and all sub dir, whose name ends in “.gsp”, will now have a creator code of “GSP+” and file type code of “GSPb”.

File Compression with Meta data

How to create a zip file that preserves Mac meta data?

For 10.6.x, in Finder, right click on a file and pull the menu “Compress ‹name›”. This will create a zip file with all meta data preserved.

In 10.4.x, the menu is “Create Archive of ‹folder name›”. The command line equivalent is: ditto -ck --sequesterRsrc --keepParent myFolder myFolder.zip.

The above method puts the resource forks and HFS meta-data into a subdirectory “__MACOSX__”. You may just want to use this instead: ditto -ck --rsrc --keepParent myFolder myFolder.zip.

http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man1/ditto.1.html

Note: Mac OS X 10.5 changed the tool it uses for dealing with zip files. Its new compression tool is The Unarchiver. For detail, see Archive Utility (aka BOMArchiveHelper).

How to unpack a zip file that has Mac meta data?

In Finder, double click on a file. The command line equivalent is ditto -xk myFolder.zip myFolder.

Does the command line program “zip” and “unzip” understand metadata?

No, as of Mac OS X 10.6.2. The command line tools at /usr/bin/zip and /usr/bin/unzip do not understand Mac meta data.

Using command line zip will simply create a archive without Mac metadata. Using unzip to unzip a zipped folder that has Mac metadata, will create a extraneous directory “__MACOSX__” and or extraneous files with names prefixed by “.”.

http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man1/zip.1.html

Does tar, gzip, gunzip understand Mac metadata?

On OS X 10.6.3, tar, gzip, gunzip, will preserve resource fork. On OS X 10.4, no. I haven't tested whether 10.6 preserves file creator and type code.

Note that if you want to preserve resource fork, you must use the version supplied with the Mac 10.6.x, at /usr/bin. This means if you have installed GNU's version (from Fink or MacPorts), remember to start you command with full path 〔/usr/bin/…〕 if you still want resource fork.

Also, if you unzip and untar using a version that does not understand resources, your resources will be lost.

For example, you can do tar cvf myfolder.tar myfolder. This will result a new file named tar cvf myfolder.tar. This file itself will have no resource fork. All the resources are embedded in the file. Now, move the tar file to a new dir, untar it by tar xvf myfolder.tar. The result dir's files will still have resource fork.

Also, it appears that, whether tar, gzip, gzip understand resource fork depends on a undocumented environment variable. For OS X 10.4, this variable is COPY_EXTENDED_ATTRIBUTES_DISABLE. It can be set to “true” or “false”, but in OS X 10.5, this variable changed name to COPYFILE_DISABLE. This info is from: 〔Resource forks and tar By Norman Walsh. @ http://norman.walsh.name/2008/02/22/tar〕 I have not verified it.

Do you have a folder with Mac metadata i can download and test?

Yes. download this file: macosx_metadata_test.zip. After download, do not use /usr/bin/unzip to decompress it, because that would lose the meta data. Use this instead:

ditto -xk macosx_metadata_test.zip  macosx_metadata_test

Thanks to Mark Reed, Kris Gybels, for corrections on the zip utility.

blog comments powered by Disqus