Open new Terminal window in current (or other specified) directory

A lot of times I find myself wanting to open another (Mac OS X) Terminal window in the same directory as my current one. This little shell script, which executes a little AppleScript, makes that trivial:

#!/bin/sh

if [ $# -ne 1 ]; then
    PATHDIR=`pwd`
else
    PATHDIR=$1
fi

osascript -e "tell application \"Terminal\"" -e "do script \"cd $PATHDIR\"" -e "end tell"

Save this script somewhere in your $PATH with executable permissions. Now instead of hitting Command-N then typing “cd really/long/path/to/your/current/directory”, you can simply type the name of the script (I used “term”):

term

Chain them together to open multiple windows. The following would open three new windows with the same current directory:

term; term; term

It can also take an optional directory path argument to override the current directory:

term /System/Library/

  • http://andr.esmejia.com Andrés Mejía

    Nice, but is there a way to bind it to the Command-T shortcut?

  • Anonymous

    This code ins’t clean or good or DRY but it does work and I don’t have any more time to mess with it. This will do the same thing but in a new tab instead of a new window.

    #!/bin/sh

    if [ $# -ne 1 ]; then
    PATHDIR=`pwd`
    else
    PATHDIR=$1
    fi

    osascript -e “Tell application “System Events” to tell process “Terminal” to keystroke “t” using command down” -e “Tell application “System Events” to tell process “Terminal” to keystroke “cd $PATHDIR”" -e “Tell application “System Events” to tell process “Terminal” to key down return” -e “Tell application “System Events” to tell process “Terminal” to keystroke “clear”" -e “Tell application “System Events” to tell process “Terminal” to key down return”