Mac OS X Resource Fork and Command Line Tips

Perm url with updates:

Mac OS X Resource Fork and Command Line Tips

Xah Lee, 2003, 2007, 2010-03-07, 2010-04-25

This document contains some tips and info related to Mac OS X's command line tools on things specific to the Mac. For example, those dealing with file type/creator, resource forks, launching GUI apps, put system to sleep, etc.

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

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's main data has 2 parts: data fork, and 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, and much more. Sometimes data in resource fork contains main content of the file. For example, Quick Time movie files used to store main movie data in the resource fork. Some image application still store thumbnail of jpg files in resource fork.

In Mac OS X, data files is not suppose to use resource forks, so that Mac's files is compatible with other OS, however, program executable files such as those “.app” may still need the resource fork intact to work, and many programs including Apple's still creates resource fork as of 2010-07. (for example, see: Safari “.webloc” File)

For detail, see: Resource fork, HFS Plus file system.

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. For example, if “27.png” is your file, and you want to know if it has resource fork, do ls -l 27.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 27.png 
-rwxrwxrwx    1 xah      staff     3184339 Jun 26  2001 27.png
$ ls -l 27.png/rsrc
-rwxrwxrwx    1 xah      staff       87410 Jun 26  2001 27.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 “27.png”. Do like this:

cp /dev/null 27.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 than rm the file. Example:

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

When using get info in Finder, it reports file size by two numbers, e.g. “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.

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

Does “cp” command copy resource fork?

Yes, as of Mac OS X 10.4

How to view resource?

Look at “/Developer/Tools/DeRez”. As of OS X 10.6, most of it is now in “/usr/bin/”. Read “man DeRez”.

File Type/Creator

File Type and Creator

What's a File Type/Creator code?

In Windows, file names has a suffix , such as “.txt”, “.jpg”, “.bat”, “.dll”, that indicates what types is the file, and what application is used when user tries to open them. (see: filename extension)

In Mac, the type of file 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 “/Developer/Tools/GetFileInfo”. e.g.

/Developer/Tools/GetFileInfo -c file.jpg
/Developer/Tools/GetFileInfo -t file.jpg

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

This tool is available on the Developers tools CD that came with Mac OS X at least 10.1 up to 10.4. Later version of OS X may have it installed by default.

GetFileInfo man page

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

Use the tool “/Developer/Tools/SetFile”. Like this:

/Developer/Tools/SetFile -type 'GSPb' -creator 'GSP+' myFile.gsp

SetFile man page

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 /Developer/Tools/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

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

ditto man page

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, seeArchive 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.

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 “.”.

zip man page

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 creater and type code, but probably yes.

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, remeber to start you command with full path 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 (2008-02), by Norman Walsh at Source. I have not verified it.

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

Yes. download this file: 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

What's “.dmg” File? How to creat it with command line?


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

Popular posts from this blog

11 Years of Writing About Emacs

does md5 creates more randomness?

Google Code shutting down, future of ErgoEmacs