Xah Lee, 2003, …, 2010-03-26
This document explains Mac OS X's “.dmg” file, and some tips about using command line tools for Mac specific things, such as launching apps, put system to sleep, etc.
“.dmg” file are files in Apple's disk image file format, The “.app” that deal with them are: Disk Copy, DiskImageMounter, Disk Utility.
What's a disk image? Why not just zip?
Disk image files are different than compressed files. Disk image is roughly a copy of the actual structure of disk. The OS will treat disk images just like a storage device, but a virtual one. For example, you can create a zip version of all files on a CD, but that zip file can't be used by the OS to boot the machine. With disk image file, it can. With disk image files, you can also create a image file of a CD, and that can be used to create a CD that is identical to the original CD.
Mac OS X's disk image file format is called “.dmg” files. For applications or executable files, it's better to use disk image format, because that way, all resource fork issues are done correctly, among other reasons.
How to create .dmg files using command line?
To create one, use hdiutil, like this:
hdiutil create -megabytes 5 -fs HFS+ -volname myDisk ~/Desktop/myDisk
This will create a disk image with 5 mega bytes, using HFS+ format (OS X standard), and the name of the disk is “myDisk”, and the disk image file created is “myDisk.dmg”.
How to open a “.dmg” file with commnand line tool?
You can just use the “open” command. Like this: open myDisk.dmg.
The disk image will show up at 〔/Volumes/〕.
You can also use “hdid”, like this:
hdid mydisk.dmg.
If it is encrypted, a password GUI dialog will pop up.
To attach a encrypted dmg file without having it pop up a GUI dialog, do hdid -stdinpass mydisk.dmg.
How to eject a “.dmg” file using command line?
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.
How to start Remote Login on the command line?
Type sudo /usr/bin/sshd. You 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.
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.
How to make Finder show hidden files?
In Terminal, type:
defaults write com.apple.finder AppleShowAllFiles TRUE killall Finder
This will set a config and restart Finder.
How to make the mouse speed faster?
defaults write -g com.apple.mouse.scaling
The above will show your current scaling value. To make it faster, do:
defaults read -g com.apple.mouse.scaling 5
You need to relog for this to take effect.
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.
How to kill the Dashboard?
You can use the command line to set a preference, then you need to relaunch Dock. Type the following code in Terminal.
defaults write com.apple.dashboard mcx-disabled -boolean YES killall Dock