Xah Lee, 2003,2007
This file 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.
For some general introduction to Mac's resource fork and HFS+, see Wikipedia: Resource fork↗, HFS Plus↗.
Is file type and creator stored in mac classic 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.
This attribute along with resource fork are both lost when transfering to other OS. When using OS X's command line tools, whether these data are kept depends on which command line tools 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. This tool is avaible on the Developers tools CD that came with Mac OS X at least 10.1 up to 10.4. Type man GetFileInfo to see its doc.
How to change a file's type and creator code using the command line?
Use the tool /Developer/Tools/SetFile. You need to install the Developers tools CD that came with OS X at least 10.1. Type man SetFile to see its doc.
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 commmand 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. (unless you append your file name with “/rsrc”, it will then get the size of just the resource fork.)
How to delete a file's resource fork programatically in the command line?
You can overwrite it with a empty file. Suppose “27.png” is a file with resource fork, and “tempf” is a empty file (size 0 bytes). Then do: cp tempf 27.png/rsrc will delete the resource fork. (to create a empty file, you can do: touch tempf)
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: touch emptyfile && cp emptyfile babe.jpg/rsrc && rm babe.jpg emptyfile.
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. The size returned by ls command does not include resource fork.
alice.jpg is a file with resource fork.
alice2.jpg is a file without resource fork.
Finder ls
alice.jpg | 584 KB, 595041 | 484995
alice2.jpg | 476 KB, 484995 | 484995
How to create a zip file that preserves Mac meta data?
In Finder, right click on a file a pull a menu «Create Archive of “my folder”», which will create a zip file but with meta data preserved. The command line equivalent is this: ditto -ck --sequesterRsrc --keepParent myFolder myFolder.zip.
The above pulls 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. See “man ditto” for detail.
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 zip and unzip understand metadata?
As of Mac OS X 10.4.9, No. 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__” or extraneous files with names prefixed by “.”.
As of Mac OS X 10.5, Mark Reed wrote me that the man page for both zip and unzip claims to understand mac metadata, but only unzip actually do.
Does command line tools gzip/gunzip and tar understand Mac metadata?
No. (as of Mac OS X 10.4.9, mid 2007)
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 instead this: ditto -xk macosx_metadata_test.zip macosx_metadata_test.
For some general introduction to Mac's disk image, see Wikipedia: .dmg↗.
How to create .dmg files using command line?
To create one, use hdiutil, e.g. hdiutil create -megabytes 1 ~/Desktop/afolder/ (this does not work with latest OS X... Read man hdiutil)
How to open a Disk Copy (.dmg) file with commnand line tool?
For Mac OS X 10.4.11 (as of 2007-12-17), to mount, do “hdid mydisk.dmg”. If it is encrypted, a password GUI dialog will pop up.
To mount a encrypted dmg file without having it popup a GUI dialog, do hdid -stdinpass mydisk.dmg. The volume should show up at “/Volumes/”.
How to eject a Disk Copy (.dmg) file using command line tool?
To eject, first find out what disk device it is associated to by typing “df”. Here's a sample output:
/dev/disk1s2 31420 27428 3992 88% /Volumes/financial
then, use hdiutil to eject it: e.g. hdiutil detach /dev/disk2s2 Note: if one uses udiutil unmount /Volumes/financialto unmount it, it leaves a hdid process uncleared, creating problem when one tries to mount the dmg file again with error about 'financial' already in use.
How to start Remote Login on the command line?
Type sudo /usr/bin/sshd. One may need to generate a passphrase if this is run for the first time. Simply go to System Prefences, Sharing, and check mark Remote Login, will automatically generate the passphrase.
How to start Windows File Sharing on the command line?
Type sudo smbd restart or sudo nmbd restart.
How to launch a Mac GUI application using command line tool?
Use “open”. e.g. open /Applications/iTunes.app. Note that the “open” command behaves as if doubling clicking on finder. In particular, if a application is already running, it will just switch to it, not launch a new process. See “man open” for more detail.
How to launch a GUI application while passing it a command line argument?
Sometimes, you want to launch a separate process of Carbon Emacs or Aquamacs, and want to pass it command line arguments. You can do like this: nohup /Applications/Emacs.app/Contents/MacOS/Emacs -q &.
Note, however, Terminal has a feature such that if you close the window, it'll quit all command launched from it, even if you have used “nohub”. The “nohub” will only prevent the app from quitting if you exit the shell by “Ctrl+d”.
How to open a URL in a running browser?
open -a safari myUrlStr. This will open the url in a currently running Safari browser if one has been launched. To specify other browsers, use “firefox” or “icab” for example.
What's the command to put system to sleep?
Use applescript, like this: osascript -e 'tell application "System Events" to sleep'
What's the command to turn on screen saver?
/System/Library/Frameworks/ScreenSaver.framework/Versions/A/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine &. You can make a bash shell alias, like this: “alias ssaver="...";” and put in “~/.bash_profile”.
You can also run the screen saver on your Desktop's background. Give the command the “-background” argument.
How to do a screenshot with command line?
/usr/sbin/screencapture. Do “man screencapture” for detail.
Related essays:
Page created: 2003. © 2005 by Xah Lee.