Have you ever wanted to open a Terminal (or iTerm) immediately into the same directory displayed in the OS X finder window? You can do this and more using Gary Kerbaugh's collection of unix utilities and Applescripts. Here is a screenshot of this in action, where I am clicking on a Finder Toolbar icon that opens a new tab of iTerm into the directory /Applications automatically:
Gary Kerbaugh has created several small unix utilities that greatly simplify and speed up terminal/finder interactions. You can download these directly from him, and install them in your directory according to his instructions (this takes about 10 seconds), or if you prefer, you can now simply use fink to install this.
These scripts include the following:
cdf and posd:
posd is an osascript embedded in a shell script that returns the pwd corresponding to the frontmost finder window. Its real value is when aliased to the command cdf, i.e., cdf='cd "$(posd)"'. The command cdf changes the terminal pwd to match that of the frontmost finder window. Two additional commands based on posd are also contained in these distributions. posfind is an extention of the unix find utility that allows you to locate files that contain a given string within the directory displayed in the finder, as well as directories underneath it. posgrep is an extension of the unix grep utility that allows you to locate a string within files in the directory displayed in the finder. Applescript applications for both of these functions are also included in Gary's distribution and in the fink distribution (see below).
fdc:
fdc is essentially the reciprocal operation of cdf. It changes the finder window to match the pwd of the terminal in which the command is issued.
Finder Toolbar Extras:
For Apple's Terminal.app
For iTerm.app
A further powerful use of posd is in conjunction with a posd.term file that one can create by saving an open terminal session after setting all defaults to the prefered state, by changing the lines in the new posd.term
<key>ExecutionString</key>
<string></string>
to the following for a tcsh terminal session:
<key>ExecutionString</key>
<string>cd "`posd`"; pwd </string>
or to the following for a zsh or bash session:
<key>ExecutionString</key>
<string>cd "$(posd)";pwd</string>
and the resulting file is a clickable icon that can be placed in the finder tool bar. Clicking opens a new terminal window corresponding to the finder.
Gary has included Applescript versions of this tool bar utility having greater functionality for both Terminal.app and iTerm.app, and these are placed in /sw/share/pos when you use the fink installation script.You can download my iTerm customization Applescripts here. This enables you to open a NEW tab in a current terminal (if one is available, otherwise it starts one) that cd's into the directory shown in the finder.
Gary also distributes an iTerm Applescript that is a little more involved and changes the directory of the current frontmost iTerm session (equivalent to typing cdf).
Two other useful OS X GUI-unix utilities: gdirs and rmm
gdirs is an alias to a zsh function that works much like the dirs command associated with pushd, but has a few advantages: It shares the directory stack between all terminal sessions, it gives a gui listing of the last 20 directories visited, in which a directory can be selected by pointing and clicking, and it enables terminal-Finder interactions. More detals are given on a separate page.
rmm is a shell script, also authored by Gary Kerbaugh, that emulates the GNU rm function, except that it moves files to the user's Trash directory, instead of unlinking them. If you alias rm to rmm for your interactive sessions (only), then it can become a rm replacement that puts everything in the Trash can, much like Finder's delete. Details and downloads here .